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 ?

4

1 回答 1

1

/proc/self/statm实际上,报告您的进程使用的虚拟内存大小。

编辑:

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

我认为这与虚拟内存有关:https ://serverfault.com/a/138435可能会有所帮助。我不认为分配数组甚至 malloc() 会给您程序实例分配的实际内存。我还会在这里查看答案:https ://stackoverflow.com/a/1237930/1767191 ,它建议您根据 proc man使用/proc/self/smapswhich 。shows memory consumption for each of the process's mappings.这意味着它将为您提供每个实例的内存消耗。

于 2013-05-07T17:24:44.103 回答