How do I get the memory usage of my c++ program? Which instruction that i can put in my program and it can tell the size of memory that my program used?
One way to go about it would be to add counters for each datatype. Initialize the counters to the number of static declarations of the datatype and increment them by one whenever memory for that datatype is allocated dynamically. In the end you can multiply the counters to the size of respective datatypes to get the memory used by that datatpe.
Add all counters to get the total memory used by the program. For example
int a,b,c,*d; char x,y; int total; int mem_int=6; //for a,b,c,mem_int,mem_char,total int mem_char=2; cin>>c; d=new int[c]; mem_int+=c; mem_int=mem_int * sizeof(int); mem_char=mem_char*sizeof(char); total=mem_int+mem_char; cout<<total;
There may be better ways of doing it, i am not sure if we have to add the memory of d or not, maybe some of the more experienced members can help out with that.
you can use Win API ::GetProcessMemoryInfo
But the system allocates memory in blocks (i don't remember exactly 4k or so) and after you allocate two bytes for example memory usage may become 4k more
Thank you everyone!!
I'm sorry forgetting to tell u that i'm running my c++ program on ubuntu.
Perhaps a debugger like Valgrind will have an option for this?
wow this getrusage() really helped me. ;)
thank you alot :D
Is there a OS independent way of getting these statistics?
AFAIK, LEDA can do it, but I need a non proprietary solution.