Unfortunately, there is no platform-independent way. If you want to measure memory usage outside the program, without changing its code, then you need to use OS specific tools.
On Linux: In Linux, how to tell how much memory processes are using?. It basically tells you to parse /proc/{the process id of the running program}/smaps
. A variant of this may work on other systems that have a /proc/
filesystem.
On Windows: How to use GetProcessMemoryInfo in C++?. It requires the HANDLE
of the process, which you can get with
handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, process_id);
PROCESS_MEMORY_COUNTERS couters;
GetProcessMemoryInfo( handle, &counters, sizeof(counters));
CloseHandle(handle);
now do something with counters ....