How can we know which process is using how much memory individually?
3 Answers
1 - Using 'ps'
This will show you top 10 process that using the most memory:
ps aux --sort=-%mem | awk 'NR<=10{print $0}'
You can manipulate the 10 if you like.
2 - Using 'top'
when you opened top press m
will short process based on memory usage.
I prefer running htop so I can watch as things change over time.
It uses a terminal window to show all the top processes, and automatically updates continually.
You can set it to sort based on CPU (default) or memory.
If it is not on your system it can be easily installed.
The problem of determining how much memory an individual process consumes is an open one, and mainly based on definition.
The issue in large part arises from shared libraries - how would you calculate the memory usage of a process which uses the same .so as 3 other processes ? would you say it consumes the entire memory the lib is allocated, as it couldn't function without it? would you say it consumes the proportional part (1/4, as there are 3 other procs which use it concurrently - pss)? would you say the process is independent of any libraries it loads in order to run?
Would you include swapped out memory, or only physical ram resident pages? What about multiple threads, or child processes? Forks use the same memory as their parent as long as no change is made - would you count that memory allocation for each process or only for the parent?
This question has been asked and answered here and in other sites many times, so I'll just give a few links and a general conclusion.
First the most quoted article on the subject - https://emilics.com/notebook/enblog/p871.html
A few SO and U&L questions on the same subject:
https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management
https://stackoverflow.com/questions/118307/a-way-to-determine-a-processs-real-memory-usage-i-e-private-dirty-rss
Correctly determining memory usage in Linux
In conclusion - /prc/[pid]/smap
as per the doc
The /proc/PID/smaps is an extension based on maps, showing the memory consumption for each of the process's mappings.
Or use the pmap command.