Need to get process memory using c++

2019-09-19 05:01发布

I want to calculate my process memory (rss) at runtime in my application (c++/unix/multithreaded).Do we have any API to use for that.Please note that , I am aware of reading /proc/stat and getrusage() , but dont want to read/parse a system file from appication and getrusage() does not work in my linux distribution.

The whole intent was to check for memory leak caused by my application . I have even tried tracking memory by overloading new/malloc/calloc/realloc and get the memory allocation trakced, but even with thsese I am not able to track the whole memory allocated by process. It would be also helpfull if you can suggest the other probable areas where I should look for memory allocation/ memory leak other than the above stated APIs. I am aware of Valgrind/mpatrol type of memory monitor tools .. but unfortunately it does not work with my application.. Thanks in advance

3条回答
戒情不戒烟
2楼-- · 2019-09-19 05:08

You can read /proc/${pid}/status, it looks like

Name:   nginx
State:  S (sleeping)
SleepAVG:   98%
Tgid:   11884
Pid:    11884
PPid:   11883
TracerPid:  0
Uid:    99  99  99  99
Gid:    99  99  99  99
FDSize: 64
Groups: 99 
VmPeak:    23932 kB
VmSize:    23932 kB
VmLck:         0 kB
VmHWM:      4276 kB
VmRSS:      4276 kB
VmData:     3744 kB
VmStk:        88 kB
VmExe:       452 kB
VmLib:      3024 kB
VmPTE:        88 kB
StaBrk: 1a931000 kB
Brk:    1a974000 kB
StaStk: 7fffc224d560 kB
Threads:    1
SigQ:   0/73712
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000040001000
SigCgt: 0000000198016a07
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,0000000f
Mems_allowed:   00000000,00000001

You can parse the VmRSS value.

查看更多
Juvenile、少年°
3楼-- · 2019-09-19 05:14

You could use valgrind. By setting it in monitor mode and calling remote method (gdb) monitor full, it would give you the total, allocated, memory at run time. See this page for more information.

查看更多
Anthone
4楼-- · 2019-09-19 05:23

First, this kind of information is operating system specific. It has to be done differently on Linux, on MacOSX, on FreeBSD...

On Linux, the blessed way, is as every one told you, to use the /proc file system, which is how all the system utilities (e.g. top or ps) are retrieving that information (perhaps by using libproc which is just a wrapper around reads of /proc/ files).

Could you explain why reading e.g. /proc/self/statm or /proc/self/stat or /proc/self/status or /proc/self/maps is not possible for you?

Remember that these /proc/files are pseudo-files, and no actual slow I/O operation to disk is involved in reading them. And you have to read them sequentially, seeking (or stat-ing) them does not work.

It seems to me that

long process_size_in_pages(void)
{
   long s = -1;
   FILE *f = fopen("/proc/self/statm", "r");
   if (!f) return -1;
   // if for any reason the fscanf fails, s is still -1,
   //      with errno appropriately set.
   fscanf(f, "%ld", &s);
   fclose (f);
   return s;
}

is the fastest way to retrieve that information. Why can't you do that?

查看更多
登录 后发表回答