How to find total memory a process is using?
-
We use a combination of malloc() and the new operator in our VC++ programs. How can we get a summary of either or both items total memory allocation? The heap walker stuff doesn't seem to do anything unless you use those dinosaur LocalAlloc() / GlobalAlloc() calls which are not portable to Unix. thanks for any help :confused:
-
We use a combination of malloc() and the new operator in our VC++ programs. How can we get a summary of either or both items total memory allocation? The heap walker stuff doesn't seem to do anything unless you use those dinosaur LocalAlloc() / GlobalAlloc() calls which are not portable to Unix. thanks for any help :confused:
Are you looking for something that is portable to unix? Here is one way (not portable), the undocumented now documented method. http://www.microsoft.com/msj/0197/hood/hood0197.aspx[^] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/ntqueryinformationprocess.asp[^] You want to use the ProcessVmCounters. 8bc7c0ec02c0e404c0cc0680f7018827ebee
-
Are you looking for something that is portable to unix? Here is one way (not portable), the undocumented now documented method. http://www.microsoft.com/msj/0197/hood/hood0197.aspx[^] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/ntqueryinformationprocess.asp[^] You want to use the ProcessVmCounters. 8bc7c0ec02c0e404c0cc0680f7018827ebee
Another method would be to use "Performance Counters" on that process. I believe there may be a few other APIs that get various information as well. 8bc7c0ec02c0e404c0cc0680f7018827ebee
-
Another method would be to use "Performance Counters" on that process. I believe there may be a few other APIs that get various information as well. 8bc7c0ec02c0e404c0cc0680f7018827ebee
Thanks Toby! There's still too much instrumentation for our simple needs. Here is what made the boss happy: C:\Temp>printmemorystats.exe total used 5054, free 760 OK - end of heap total used 10174, free 3816 OK - end of heap total used 12222, free 1760 OK - end of heap total used 16318, free 5848 OK - end of heap total used 24510, free 9936 OK - end of heap total used 40894, free 14024 OK - end of heap total used 73662, free 22128 OK - end of heap total used 139198, free 26216 OK - end of heap total used 270270, free 30304 OK - end of heap total used 532414, free 34392 OK - end of heap
#include #include static void PrintMemoryStats(); int main() { int sizemem=1024; int ii; char *foo; for (ii=0; ii<10; ii++) { PrintMemoryStats(); foo = malloc(sizemem); sizemem *= 2; } } static void PrintMemoryStats() { _HEAPINFO hinfo; int heapstatus; long totalUsed = 0, totalFree = 0; hinfo._pentry = NULL; while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK ) { if (hinfo._useflag == _USEDENTRY) totalUsed += hinfo._size; else if (hinfo._useflag == _FREEENTRY) totalFree += hinfo._size; } printf ("total used %ld, free %ld\n", totalUsed, totalFree); switch( heapstatus ) { case _HEAPEMPTY: printf( "OK - empty heap\n" ); break; case _HEAPEND: printf( "OK - end of heap\n" ); break; case _HEAPBADPTR: printf( "ERROR - bad pointer to heap\n" ); break; case _HEAPBADBEGIN: printf( "ERROR - bad start of heap\n" ); break; case _HEAPBADNODE: printf( "ERROR - bad node in heap\n" ); break; } }
-- modified at 15:25 Friday 27th January, 2006 -
We use a combination of malloc() and the new operator in our VC++ programs. How can we get a summary of either or both items total memory allocation? The heap walker stuff doesn't seem to do anything unless you use those dinosaur LocalAlloc() / GlobalAlloc() calls which are not portable to Unix. thanks for any help :confused:
See if this helps.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli