0

I was trying to track how much memory my application is taking. So I was reading /proc/self/statm.

#include <iostream>
#include <fstream>

void print_mem(){
  std::ifstream proc_stream("/proc/self/statm");
  long long VmSize = 0, VmRSS = 0, Share = 0;
  proc_stream >> VmSize >> VmRSS >> Share;
  proc_stream.close();
  std::cout << VmSize << " " << VmRSS << std::endl;
}

struct C{
    int a[256];
};

int main(){
    print_mem();// first call
    C* c = new C;
    print_mem();// second call
    return 0;
}

I was expecting there will be some growth in VmSize. But What I see is it always reports same VmSize, VmRSS. shouln't it change as I've allocated c ?

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • It is reporting. I just attempted to malloc() 500,000 bytes (~0.5 MB) and VmSize jumps from 874 to 998. – Maggy May May 07 '13 at 17:19
  • I set `a[4096]` instead of `256` But I don't see any change. However If I change it to `a[1024*1024]` I see a change from `756` to `1782`. – Neel Basu May 07 '13 at 17:23

1 Answers1

1

/proc/self/statm is, in fact, reporting the virtual memory size used by your process.

Edit:

I set a[4096] instead of 256 But I don't see any change. However If I change it to a[1024*1024] I see a change from 756 to 1782

I think it has to do with what virtual memory is: https://serverfault.com/a/138435 might be of help. I don't think allocating an array or even a malloc() is giving you the actual memory allocated by the instance of the program. I would also check out the answer here: https://stackoverflow.com/a/1237930/1767191 which suggests you use /proc/self/smaps which shows memory consumption for each of the process's mappings. according to the proc man. This means it will give you memory consumption per instance.

Community
  • 1
  • 1
Maggy May
  • 233
  • 1
  • 7