Peak Memory Usage of a Linux/Unix Process

Peak memory usage of a linux/unix process

Here's a one-liner that doesn't require any external scripts or utilities and doesn't require you to start the process via another program like Valgrind or time, so you can use it for any process that's already running:

grep ^VmPeak /proc/$PID/status

(replace $PID with the PID of the process you're interested in)

How can I determine max memory usage of a process in Linux?

Valgrind's massif tool can give you a chart of memory usage over time. See http://valgrind.org/docs/manual/ms-manual.html

How can I measure the actual memory usage of an application or process?

With ps or similar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:

  • does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it

  • can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries

If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, Valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of Valgrind is called 'massif':

Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

As explained in the Valgrind documentation, you need to run the program through Valgrind:

valgrind --tool=massif <executable> <arguments>

Massif writes a dump of memory usage snapshots (e.g. massif.out.12345). These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print, a simple text-based tool shipped with Valgrind, to be of great help already.

To find memory leaks, use the (default) memcheck tool of valgrind.

Getting the max CPU / RAM usage for a process in Linux

"Maximum CPU usage" is essentially meaningless. At any instant in time, a process is either running on one (or more) CPUs, or is not running. As such, the maximum CPU usage of any process will always be at least 100%, because there was at least one instant at which the process is running. Tools which display CPU usage as a percentage are measuring the ratio of CPU time consumed by a process to its age.

The only situation where this might be a useful measure would be for multithreaded processes, where the "maximum CPU usage" is the maximum number of threads belonging to the process which are all running simultaneously. I'm not aware of any specific way to measure this.

Is there a way to measure how much a shell command used memory?

I was looking for something like this.

This is how I used it:

$/usr/bin/time -f "%E %M" *<command>*

That gave me:

time (%E) - elapsed real time (in [hours:]minutes:seconds);

memory (%M) - maximum resident set size of the process during its lifetime, in kB.

Limit memory usage for a single Linux process

There's some problems with ulimit. Here's a useful read on the topic: Limiting time and memory consumption of a program in Linux, which lead to the timeout tool, which lets you cage a process (and its forks) by time or memory consumption.

The timeout tool requires Perl 5+ and the /proc filesystem mounted. After that you copy the tool to e.g. /usr/local/bin like so:

curl https://raw.githubusercontent.com/pshved/timeout/master/timeout | \
sudo tee /usr/local/bin/timeout && sudo chmod 755 /usr/local/bin/timeout

After that, you can 'cage' your process by memory consumption as in your question like so:

timeout -m 500 pdftoppm Sample.pdf

Alternatively you could use -t <seconds> and -x <hertz> to respectively limit the process by time or CPU constraints.

The way this tool works is by checking multiple times per second if the spawned process has not oversubscribed its set boundaries. This means there actually is a small window where a process could potentially be oversubscribing before timeout notices and kills the process.

A more correct approach would hence likely involve cgroups, but that is much more involved to set up, even if you'd use Docker or runC, which among things, offer a more user-friendly abstraction around cgroups.

Max memory usage of different process in a single bash file

/usr/bin/time -f "process 1 max RSS %M kbytes" ./program1 parameterSet1
/usr/bin/time -f "process 2 max RSS %M kbytes" ./program2 parameterSet2

Will put the maximum resident set size on stderr for the program. You can tailor the format string (as I did) or redirect outputs to trace each separate process.



Related Topics



Leave a reply



Submit