I'm writing a program on Ubuntu 16.04, and it will fork, do some setups and exec another program. I need to:

  1. Limit the maximum VSS the program could use
  2. Limit the maximum RSS the program could use
  3. Measure the maximum VSS the program used during its execution
  4. Measure the maximum RSS the program used during its execution

So far I can do 1. and 4. as following:

pid_t chpid = fork();
if (!chpid) {
    // I do lots of setup here
    struct rlimit rlim;
    rlim.rlim_cur = rlim.rlim_max = limit_VSS;
    setrlimit(RLIMIT_AS, &rlim); // 1.
    execv(path, args);
struct rusage stats;
wait3(NULL, 0, &stats);
long max_RSS = stats.ru_maxrss;  // 4.

How can I implement 2. and 3.?

The RLIMIT_RSS option in setrlimit seems not useful, and I don't know how to get VmPeak in /proc/pid/status after the process is terminated (or right before the process terminates).

I need a efficient way, i.e., not having a significant impact on the exec program. (There are ptrace and some tools such as valgrind, but it will slow down the execution a lot.)

  • 2
    Sorry, what is VSS and RSS? – 111--- Dec 28 '17 at 18:55
  • @datUser Virtual Set Size and Resident Set Size. Explanation. – Colera Su Dec 29 '17 at 0:37

I don't know about separating out VSS from RSS usage, but you could look at using memory pools, or using a malloc substitute which monitors and/or enforces maximum allocation from the system.

Take a look at the taskstats process accounting package. It doesn't record final memory usage, but does record High watermark of RSS usage and VM usage, which may be what you want anyway? I'm not sure if it's available as standard with Ubuntu's 16.04 kernels - you may need to build a kernel module to get it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.