views:

658

answers:

4

I have a long-running memory hog of an experimental program, and I'd like to know it's actual memory footprint. The Task Manager says (in windows7-64) that the app is consuming 800 mb of memory, but the total amount of memory allocated, also according to the task manager, is 3.7gb. The sum of all the allocated memory does not equal 3.7gb. How can I determine, on the fly, how much memory my application is actually consuming.

Corollary: What memory is the task manager actually reporting? It doesn't seem to be all the memory that's allocated to the app itself.

A: 

That depends on what memory you are talking about. Unfortunately there are many different ways to measure memory. For instance ...

  • Physical Memory Allocated
  • Virtual Memory Allocated
  • Virtual Memory Reserved (but not committed)
  • Private Bytes
  • Shared Bytes

Which metric are you interested in?

I think most people tend to be interested in the "Virtual Memory Allocated" category.

JaredPar
Is that because you can't typically control when memory gets swapped to disk? Because it seems Physical Memory Allocated is the one that causes the problems. I am not clear on this inter-relationship myself...
RedFilter
@OrbMan, I'm not sure about the first part. I wouldn't count out some API existing which says "please don't swap out this page of memory" But I don't know of one off hand
JaredPar
@JaredPar: I'm interested in how much of the 64 bit address space has been allocated, not necessarily where that memory got allocated. As in, how many and how large are my arrays? Did I make some mistake in not removing one or two of those that I thought I already had?
mmr
+1  A: 

As I understand it, Task manager shows the Working Set;

working set: The set of memory pages recently touched by the threads of a process. If free memory in the computer is above a threshold, pages are left in the working set of a process even if they are not being used. When free memory falls below a threshold, pages are trimmed from the working set.

via http://msdn.microsoft.com/en-us/library/cc432779(PROT.10).aspx

You can get Task Manager to show Virtual Memory as well.

I usually use perfmon (Start -> Run... -> perfmon) to track memory usage, using the Private Bytes counter. It reflects memory allocated by your normal allocators (new/HeapAlloc/malloc, etc).

Kim Gräsman
@Kim: So, it's not a good way to know how much memory is allocated to a particular application, then, just how much has been touched 'recently', for a given value of 'recently'
mmr
That's the idea, yes.
Kim Gräsman
@Kim: Looks like Perfmon is what I need, thanks!
mmr
A: 

Memory is a tricky thing to measure. An application might reserve lots of virtual memory but not actually use much of it. Some of the memory might be shared; that is, a shared DLL might be loaded in to the address space of several applications but it is only loaded in to physical memory once.

A good measure is the working set, which is the set of pages in its virtual address space that have been accessed recently. What the meaning of 'accessed recently' is depends on the operating system and its page replacement algorithm. In other words, it is the actual set of virtual pages that are mapped in to physical memory and are in use at the moment. This is what the task manager shows you.

The virtual memory usage is the amount of virtual pages that have been reserved (note that not all of these will have actually been committed, that is, had physical backing store allocated for it. You can add this to the display in task manager by clicking View -> Select Columns.

The most important thing though: If you want to actually measure how much memory your program is using to see if you need to optimize some of it for space or choose better data structures or persist some things to disk, using the task manager is the wrong approach. You should almost certainly be using a profiler.

IRBMe
I could eventually use a profiler, but I want to see how much memory is being used without the slowdown of the profiler in the app itself. Eventually, it will be necessary, but just for the here-and-now of how much memory is actually being consumed, it looks like perfmon is the right tool.
mmr
A: 

The memory statistics displayed by task manager are not nearly all the statistics available, nor are particularly well presented. I would use the great free tool from Microsoft Sysinternals, VMMap, to analyse the memory used by the application further.

If it is a long running application, and the memory usage grows over time, it is going to be the heap that is growing. Parts of the heap may or may not be paged out to disk at any time, but you really need to optimize you heap usage. In this case you need to be profile your application. If it is a .Net application then I can recommend Redgate's ANTS profiler. It is very easy to use. If it's a native application, then the Intel vtune profiler is pretty powerful. You don't need the source code for the process you are profiling for either tool.

Both applications have a free trial. Good luck.

P.S. Sorry I didn't include more hyperlinks to the tools, but this is my first post, and stackoverflow limits first posts to one hyperlink :-(