Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to find the memory usage programatically?

Hi
I want to find what is the memory usage my application
eg. myapp.exe is the output of myapp.cpp.
when the exe is running it has to print current memory usages at certain execution point.

//myapp.cpp
#iclude <iostream.h>
long getMemStatus()
{
//this method has to find what is the size of memory usage in its current process space
}

void main ()
{

cout<<"\n1. current mem usage:"<<getMemstaus();
int *i=new int;
cout<<"\n2. current mem usage:"<<getMemstaus();
int x=10;
cout<<"\n3. current mem usage:"<<getMemstaus();
delete i;
cout<<"\n4. current mem usage:"<<getMemstaus();
}

can somebody write the code for the function above getMemStatus()?



Also how to find what is heap mem size, stack mem size for my process programatically?
[914 byte] By [anbumani] at [2007-11-11 6:33:52]
# 1 Re: How to find the memory usage programatically?
dude dont post your cd key if thats what that is. Danny remove that?
jonnin at 2007-11-11 21:03:06 >
# 2 Re: How to find the memory usage programatically?
dude dont post your cd key if thats what that is. Danny remove that?
I just have removed it.
Danny at 2007-11-11 21:04:06 >
# 3 Re: How to find the memory usage programatically?
you can overload opertor 'new' and 'delete' to determine how much memory are used in your app.
Butterfly at 2007-11-11 21:05:05 >
# 4 Re: How to find the memory usage programatically?
it's a very, very platform dependent issue. Most operating system these days use virtual memory management which means that the memory your program accesses is an alias of the real physical addresses of raw memory. This also means that you cannot tell whether the memory allocated by new is located on the RAM, the process's swap file, or the cache memory.
Depending on the platform you're using, there are certain APIs that can give you a clue but not much more than a clue because your process uses various sources of memory: its private heap, memory shared with one or more processes, memory mapped files etc.
Danny at 2007-11-11 21:06:05 >
# 5 Re: How to find the memory usage programatically?
"Working Set Memory: " + (System.Environment.WorkingSet/1024).ToString() + "kb";
jrmno1 at 2007-11-11 21:07:10 >