Solved

How to measure memory usage

Posted on 2009-04-14
11
1,893 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
valid enum? 6 73
change colour of repeater control in asp.net c# 7 73
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 96
Beginner to Unreal Engine 4 5 61
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 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

22 Experts available now in Live!

Get 1:1 Help Now