Memory related frequently asked questions.
How to find the memory used by a program?
There are several methods to find the memory used by a program.
Most commonly used command for this purpose is ps.
Following are the values that could tell us about memory from ps -aux command
VSZ is Virtual Set Size, the total virtual memory size of the process. ie. the total amount of memory in swap and RAM.
RSS is Resident Set Size, the non-swapped physical memory used by process.
Most programs running in linux uses shared libraries. Please note that VSZ and RSS will be showing the memory used by shared libraries along with the process. A shared library used by one process could be used by another process also. This would result in the erroneous calculation of memory.
So for getting a better idea of the memory consumed by a process, pmap command could be used.
With pmap command, you can see the memory utilization of all libraries and process separately, which will help in tracking down the exact memory used by a process.
Why do some processes show "Threads:" to be more than 1 and other known multithreaded processes only shows "Threads: 1" in the output of /proc/<pid>/status?
Threads in /proc/pid/status shows the number of kernel threads owned by the light weight process. While a thread can either be managed at the application level or by the kernel, Light weight process is always managed by the kernel and it is scheduled as a regular process.
Lightweight processes would posses multiple threads and each of the threads in this process shares the same address space. OS will be only seeing one process. In other multithreaded applications, new processes are created in parallel with its own address space. So multiple processes will be seen in the output of ps.
Using a command like following, the number of threads running on a light weight process will be displayed.
How can I set memory allocation for an individual process?
Memory limits of a process can be set in /etc/security/limits.conf file, based on the user running the process.
You can add something like following in /etc/security/limits.conf file.
<username> hard rss 5000
Check the man page for more info.