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?
Thank you
since you are on Ubuntu, the most trustworthy numbers come from the OS: the process’s resident set size (RSS). Counting your own allocations (as suggested) will not reflect allocator overhead, alignment, thread stacks, or what the kernel actually maps. And yes, small allocations can bump usage by a full page, as noted. There is no standard C++ API for this; you need per-OS calls.
Here is a minimal Linux-friendly helper that reports current RSS and peak RSS in bytes. It uses /proc/self/statm for current RSS and getrusage() for the high-water mark. Note the unit differences for ru_maxrss.
#include <sys/resource.h>
#include <unistd.h>
#include <cstdio>
#include <cstddef>
static size_t peak_rss_bytes() {
rusage ru{};
if (getrusage(RUSAGE_SELF, &ru) == 0) {
#ifdef __APPLE__
return (size_t)ru.ru_maxrss; // bytes on macOS
#else
return (size_t)ru.ru_maxrss * 1024; // kilobytes on Linux
#endif
}
return 0;
}
static size_t current_rss_bytes() {
#if defined(__linux__)
long dummy = 0, rss_pages = 0;
if (FILE* f = std::fopen("/proc/self/statm", "r")) {
if (std::fscanf(f, "%ld %ld", &dummy, &rss_pages) != 2) rss_pages = 0;
std::fclose(f);
}
return (size_t)rss_pages * (size_t)sysconf(_SC_PAGESIZE);
#else
return 0; // use your platform's API here
#endif
} On Linux, ru_maxrss is in kilobytes; on macOS it is bytes. /proc/self/statm reports page counts; multiply by page size for bytes. (man7.org)
For a portable workflow (answering ): there is no OS-independent call in standard C++. Use OS APIs behind #ifdef (e.g., Windows GetProcessMemoryInfo), or profile externally. For detailed heap timelines on Ubuntu, valgrind --tool=massif ./your_app then ms_print massif.out.* gives peak and breakdowns; add --stacks=yes if you also want stack usage. (learn.microsoft.com)
Jump to Post— hammerhead 19One 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 …
Jump to Post— mitrmkar 1,056see getrusage(), e.g. here
http://linux.die.net/man/2/getrusage
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.