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.
In recent versions of linux, use the smaps subsystem. For example, for a process with a PID of 1234:
It will tell you exactly how much memory it is using at that time. More importantly, it will divide the memory into private and shared, so you can tell how much memory your instance of the program is using, without including memory shared between multiple instances of the program.
While this question seems to be about examining currently running processes, I wanted to see the peak memory used by an application from start to finish. Besides valgrind, you can use tstime, which is much simpler. It measures the "highwater" memory usage (RSS and virtual). From this answer.
Use the in-built 'system monitor' GUI tool available in ubuntu
There isn't a single answer for this because you can't pin point precisely the amount of memory a process uses. Most processes under linux use shared libraries. For instance, let's say you want to calculate memory usage for the 'ls' process. Do you count only the memory used by the executable 'ls' ( if you could isolate it) ? How about libc? Or all these other libs that are required to run 'ls'?
You could argue that they are shared by other processes, but 'ls' can't be run on the system without them being loaded.
Also, if you need to know how much memory a process needs in order to do capacity planning, you have to calculate how much each additional copy of the process uses. I think /proc/PID/status might give you enough info of the memory usage AT a single time. On the other hand, valgrind will give you a better profile of the memory usage throughout the lifetime of the program
Try the pmap command:
If your code is in C or C++ you might be able to use
getrusage()
which returns you various statistics about memory and time usage of your process.Not all platforms support this though and will return 0 values for the memory-use options.
Instead you can look at the virtual file created in
/proc/[pid]/statm
(where[pid]
is replaced by your process id. You can obtain this fromgetpid()
).This file will look like a text file with 7 integers. You are probably most interested in the first (all memory use) and sixth (data memory use) numbers in this file.