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
psis "wrong"Depending on how you look at it,
psis 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 bypsare almost definitely wrong.
(Note: This question is covered here in great detail.)
With
psor 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)
memchecktool of valgrind.Newer tools I did not try myself are HeapTrack and the heap profiler in gperftools.