Solved

How to measure memory usage

Posted on 2009-04-14
11
1,892 Views
Last Modified: 2013-12-14
Hi,
 I need to measure the size of allocated memory by my C++ application to be sure there is no more memory leakage. Is it possible by Window's Task Manager or by any other tool? What column should I watch?
My OS is Windows XP.
0
Comment
Question by:ToL
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24135872
>> Is it possible by Window's Task Manager

Sure, if you just want to see if the memory usage keeps increasin, when it shouldn't, you can use the task manager. Just check on the Processes tab, in the Mem Usage column (you might have to enable the column using View > Select Columns).

Alternatively, if what you need is a more in-depth analysis of the memory usage, with tools to help you find memory leaks, then consider using a proper memory debugger, like Insure++, or Purify, or ...
0
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24135887
If this is a linux environment, using gnu gcc, then Valgrind is an excellent free memory profiling tool.
0
 

Author Comment

by:ToL
ID: 24135939
I have been watching Mem Usage column, but the results were strange. Is the Mem Usage the total memory used by the process? Does it works when part of the memory is swapped? What is VM Size column?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24135944
>> If this is a linux environment,

>> My OS is Windows XP.

;) Otherwise I would surely have mentioned Valgrind heh, but unfortunately it's not available for Windows.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 20 total points
ID: 24136119
>> Is the Mem Usage the total memory used by the process?

That should be the total amount of memory that the process is currently using, yes.


>> What is VM Size column?

That would be the part of the process's memory that has been paged to the disk (to the page file).
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24136150
>> My OS is Windows XP.

DOH!  My bad.

The memory shown in the task manager, is the total amount of memory that the OS has assigned to your application.  You can use this, to observe when you application frees memory, and when it mallocs memory, but it cannot tell you much else.  If you have an application that dynamically mallocs/frees alot, it can be hard to detect small memory leaks, as the memory bounces up and down.

If your application is rather static in how it allocates memory, you may be able to observe a steady growth of allocated memory that _may_ indicate a memory leak.

You are much better off using developer tools and memory profiler tools to actually observe and find memory leaks though.
0
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24136160
And what do you mean when you say the results are strange?  What were the results, and what results were you expecting?
0
 

Author Comment

by:ToL
ID: 24136428
I have created a test - allocate few MBs. Mem Usage was not changed, while VM Size was incremented. After delete the allocated memory the VM Size decremented to the previous value. So it seems the VM size is what I am looking for, I just wanted to be sure it is reliable.
To detect memory leaks I am using overloaded operators new and delete, it works. My code is probably ok, but I am using some libraries and my application is running as a dll plugin inside another one, so it is a little bit complicated...
0
 
LVL 7

Assisted Solution

by:HalfAsleep
HalfAsleep earned 25 total points
ID: 24136514
Well, I know that personally, I would not trust the Task Manager for something as critical as memory leak verification.  Reliable, probably.  How to interpret what you see?  That is more difficult.

If you have overloaded new and delete, and are doing your own memory tallying, then I wold trust that more than I would trust the task manager.  As you said, your application is run in a special way.  How can you then use the task manager?  If you suspect a memory leak from looking at the task manager, it may not even be in your code, it may be in the libraries you use.

Why don't you use some form of statistics in your own code, for the new and delete, and then your code can report any outstanding allocated memory at exit.  This would be much more reliable than the task manager.  Doing that, and also using some developer tools, would be a lot more reliable and efficient than using the task manager.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 80 total points
ID: 24138301
The simplest way to check if your code is leaking without resorting to memory profiling is just to enable the CRT (C Runtime) memory debug tools. Then, when you run your application you'll get a report of what, if any, memory leaks were detected. You can then set break-points on the allocations to see where the leak starts from.

You can use _CrtSetDbgFlag to enable CRT heap allocation debugging. This should be at the very start of your program.
E.g. _CrtSetDbgFlag ( _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_LEAK_CHECK_DF );
http://msdn2.microsoft.com/en-us/library/974tc9t1(VS.80).aspx

You can use _CrtDumpMemoryLeaks to generate an error report if the application failed to free all the memory it allocated. This should be at the very end of your program.
http://msdn2.microsoft.com/en-us/library/d41t22sb(VS.80).aspx

Use can use _CrtSetBreakAlloc or _crtBreakAlloc to set break points where specific heap is allocated (as reported by CrtDumpMemoryLeaks) so that you can see where the problem starts
http://support.microsoft.com/kb/151585

Memory leak detection and isolation: http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx

The memory usage shown my Task Manager is not accurate (I had a link to a great article but right now I can't find it but if I do I'll be sure to post it for you). If you just want to monitor memory usage then consider using Perfmon, which is a tool that comes with Windows for monitor performance, including memory usage, of a process. You probably want to watch private bytes.
http://technet.microsoft.com/en-us/library/bb490957.aspx
http://msdn.microsoft.com/en-us/library/aa645516.aspx

Alternatively, the excellent Process Explorer can be used to perform quick memory usage checked.
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
0
 

Author Comment

by:ToL
ID: 24192237
Thank you for useful advices. I was able to discover memory leakage in library I use by watching VM Size column in Task Manager.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now