19

How do I measure the peak memory of an application running on Linux?

I run this app in batch so I cannot use RSS since, it reports the current memory. I need the peak memory that the application has used to be reported at the end.

The VmPeak is not a solution either, since it reports the allocated memory and also does not calculates from the real Ram but also from the Hard Disk.

3

2 Answers 2

20

Here are 2 methods for tracking a processes' peak memory usage.

Syrupy

I haven't used this tool but it sounds like what you're looking for. It's called Syrupy.

Description

Syrupy is a Python script that regularly takes snapshots of the memory and CPU load of one or more running processes, so as to dynamically build up a profile of their usage of system resources.

Example

$ syrupy.py myprog

  PID DATE        TIME     ELAPSED  CPU   MEM    RSS   VSIZE
14634 2008-10-10  20:45:25   00:00  0.0   0.0   2996    6680
14634 2008-10-10  20:45:26   00:01  105   0.2   7804   12592
14634 2008-10-10  20:45:27   00:02  103   0.2   8996   13776
14634 2008-10-10  20:45:28   00:03  103   0.2  10468   15348
14634 2008-10-10  20:45:29   00:04  103   0.3  11412   16396
14634 2008-10-10  20:45:30   00:05  104   0.3  12492   17444

/usr/bin/time -v

Yes ironically the GNU time command can give you the peak memory usage of a process. It reports the peak memory like so: Maximum resident set size (kbytes).

Example

$ /usr/bin/time -v ~/projects/prime_numbers/eratosthenes_prime_sieve.pl 10 1000000
...

    Command being timed: "/home/saml/projects/prime_numbers/eratosthenes_prime_sieve.pl 10 1000000"
    User time (seconds): 1.12
    System time (seconds): 0.05
    Percent of CPU this job got: 54%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.19
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 79304
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 20014
    Voluntary context switches: 83
    Involuntary context switches: 274
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

References

6

Even though the topic is quite old, I want to share another project that emerged from the cgroups Linux kernel feature.

https://github.com/gsauthof/cgmemtime:

cgmemtime measures the high-water RSS+CACHE memory usage of a process and its descendant processes.

To be able to do so it puts the process into its own cgroup.

For example process A allocates 10 MiB and forks a child B that allocates 20 MiB and that forks a child C that allocates 30 MiB. All three processes share a time window where their allocations result in corresponding RSS (resident set size) memory usage.

The question now is: How much memory is actually used as a result of running A?

Answer: 60 MiB

cgmemtime is the tool to answer such questions.

Usage examples would be:

$ sudo ./cgmemtime --setup -g <myusergroup> --perm 775

$ ./cgmemtime ./testa x 10 20 30
Parent PID is 27189
Allocating 10 MiBs
New Child: 27193
Allocating 20 MiBs
New Child: 27194
Allocating 30 MiBs
Child user:    0.000 s
Child sys :    0.005 s
Child wall:    6.006 s
Child high-water RSS                    :      11648 KiB
Recursive and acc. high-water RSS+CACHE :      61840 KiB

$ ./cgmemtime python -c 'print range(100000)[48517]'
48517
Child user:    0.014 s
Child sys :    0.014 s
Child wall:    0.029 s
Child high-water RSS                    :       9948 KiB
Recursive and acc. high-water RSS+CACHE :       5724 KiB
7
  • So, how could this tool be used to report the peak RAM usage of a program after that program has exited?
    – terdon
    Commented Mar 23, 2015 at 19:23
  • @terdon It uses wait4 to catch process exit in a combination with cgroups (memory.max_usage_in_bytes), which seems to make sense. More details are available in the README on github. Commented Mar 23, 2015 at 19:37
  • Thanks, but my point was more about your answer. As it stands, it is not answering the question, only pointing to some external resource that might be able to. Also, you've posted the exact same answer elsewhere which raises an automatic flag. Please edit your answer and include an example showing how this program can do what the OP is asking for.
    – terdon
    Commented Mar 23, 2015 at 19:40
  • 2
    @terdon Yes, I did! Because I have searched for the same question and the questions are pretty much the same, and answers are all the same: 'parse ps output', which is crazy! I spent a week before accidentally bumped into cgmemtime project, which is the perfect match for all those questions, that I have commented on! Commented Mar 23, 2015 at 19:48
  • 1
    Fair enough. It would just be great if you could give an example of how it could be used. It would improve your answer. In any case, thanks for taking the time to post this, I'm sure others will find it useful.
    – terdon
    Commented Mar 23, 2015 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .