Get memory high water mark for a time interval

Viewed 1125

I'm trying to get the max amount of memory used during brief intervals, for a long-running linux process. For example, something like:

resetmaxrss(); // hypothetical new command
void* foo = malloc(4096);
free(foo);
getrusage(...); // 'ru_maxrss' reports 4096 plus whatever else is alive

resetmaxrss();
void* bar = malloc(2048);
free(bar);
getrusage(...); // 'ru_maxrss' reports 2048 + whatever, *not* 4096

Options I've found and ruled out:

  • getrusage's max RSS can't be reset.
  • cgmemtime seem to use wait4 under the hood, so isn't viable to query a process while it's running.
  • tstime reports for exiting processes, so is also not viable to query a process while it's running.

Other options, none of which are good:

  • Polling. Prone to miss our brief allocations.
  • Instrumenting our code. We don't have access to all of the memory allocators being used, so this wouldn't be very elegant or straightforward. I'd also rather use values reported by the OS for accuracy.

Is there a way to do this, short of proposing a patch to the Linux kernel?

1 Answers
Related