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.
Beside the solutions listed in thy answers, you can use the Linux command "top"; It provides a dynamic real-time view of the running system, it gives the CPU and Memory usage, for the whole system along with for every program, in percentage:
to filter by a program pid:
to filter by a program name:
"top" provides also some fields such as:
VIRT -- Virtual Image (kb) :The total amount of virtual memory used by the task
RES -- Resident size (kb): The non-swapped physical memory a task has used ; RES = CODE + DATA.
DATA -- Data+Stack size (kb): The amount of physical memory devoted to other than executable code, also known as the 'data resident set' size or DRS.
SHR -- Shared Mem size (kb): The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.
Reference here.
Edit: this works 100% well only when memory consumption increases
If you want to monitor memory usage by given process (or group of processed sharing common name, e.g.
google-chrome
, you can use my bash-script:this will continously look for changes and print them.
Valgrind can show detailed information but it slows down the target application significantly, and most of the time it changes the behavior of the app.
Exmap was something I didn't know yet, but it seems that you need a kernel module to get the information, which can be an obstacle.
I assume what everyone wants to know WRT "memory usage" is the following...
In linux, the amount of physical memory a single process might use can be roughly divided into following categories.
M.a anonymous mapped memory
M.n named mapped memory
Utility included in Android called showmap is quite useful
I am using Arch Linux and there's this wonderful package called
ps_mem
Example Output
I would suggest that you use atop. You can find everything about it on this page. It is capable of providing all the necessary KPI for your processes and it can also capture to a file.
If you want something quicker than profiling with Valgrind and your kernel is older and you can't use smaps, a ps with the options to show the resident set of the process (with
ps -o rss,command
) can give you a quick and reasonable_aproximation_
of the real amount of non-swapped memory being used.