Memory usage of current process in C

后端 未结 7 1495
深忆病人
深忆病人 2020-12-01 04:22

I need to get the memory usage of the current process in C. Can someone offer a code sample of how to do this on a Linux platform?

I\'m aware of the cat /proc/

7条回答
  •  借酒劲吻你
    2020-12-01 04:56

    I came across this post: http://appcrawler.com/wordpress/2013/05/13/simple-example-of-tracking-memory-using-getrusage/

    Simplified version:

    #include 
    #include 
    
    int main() {
      struct rusage r_usage;
      getrusage(RUSAGE_SELF,&r_usage);
      // Print the maximum resident set size used (in kilobytes).
      printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
      return 0;
    }
    

    (tested in Linux 3.13)

提交回复
热议问题