This question is covered here in great detail.
How do you measure the memory usage of an application or process in Linux?
From the blog article of Understanding memory usage on Linux, ps
is not an accurate tool to use for this intent.
Why
ps
is "wrong"Depending on how you look at it,
ps
is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported byps
are almost definitely wrong.
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':As explained in the valgrind documentation, you need to run the program through valgrind:
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 foundms_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.Check shell script to check memory usage by application in linux. Also available on github and in a version without paste and bc.
Use smem, which is an alternative to ps which calculates the USS and PSS per process. What you want is probably the PSS.
USS - Unique Set Size. This is the amount of unshared memory unique to that process (think of it as U for unique memory). It does not include shared memory. Thus this will under-report the amount of memory a process uses, but is helpful when you want to ignore shared memory.
PSS - Proportional Set Size. This is what you want. It adds together the unique memory (USS), along with a proportion of its shared memory divided by the number of other processes sharing that memory. Thus it will give you an accurate representation of how much actual physical memory is being used per process - with shared memory truly represented as shared. Think of the P being for physical memory.
How this compares to RSS as reported by ps and other utilties:
Notice: smem can also (optionally) output graphs such as pie charts and the like. IMO you don't need any of that. If you just want to use it from the command line like you might use ps -A v, then you don't need to install the python-matplotlib recommended dependency.
Use this as root and you can get a clear output for memory usage by each process.
OUTPUT EXAMPLE:
Below command line will give you the total memory used by the various process running on the Linux machine in MB
A good test of the more "real world" usage is to open the application, then run
vmstat -s
and check the "active memory" statistic. Close the application, wait a few seconds and runvmstat -s
again. However much active memory was freed was in evidently in use by the app.