Do you know what is VIRT(virtual memory), RES(resident memory) and SHR(shared memory) really mean in top command? - Let's find out.
Resident Memory- It is part of the RAM currently used by the process. RAM is logically divided into memory pages of certain size(for ex: 4096 bytes- 4 kb), memory is assigned to a process in terms of memory pages. A memory page can be associated with one process(if page is not shared) or multiple process(if a page is shared). The number of memory pages used by a process defines the resident memory(RES) it use. If you see first process(pid 25390) in top output, which is taking 933620 KB of resident memory(RES) the number of memory pages it is currently using can be calculated as:
Number of pages used = memory used(in KB)/Page size(in KB) - Formula 1
Number of pages used = memory used(in KB)/Page size(in KB) - Formula 1
As per this formula, first process(pid 25390) is using 233405(933620 KB/4 KB) memory pages currently. Note that page size of the machine on which top command is run is 4 KB.
Note that resident memory for a process can vary because memory pages might be swapped in or out.
Note that resident memory for a process can vary because memory pages might be swapped in or out.
Virtual memory(VIRT) - It is the full size of the memory process is using, whether in RAM or on disk (shared objects, mmaped files, swap area) so virtual memory is always larger or equal to resident memory.
Note that operating system doesn't load complete process into memory(RAM) as process size could be huge and a single process size could be greater than or equal to complete RAM size which will not allow other processes to execute. So to run multiple process together, operating system allocate only few pages of memory to a process initially, it loads rest of the pages related to the process only when required. So resident memory is RAM currently used by the process whereas virtual memory is total memory of the process.
Total virtual memory pages used by first process = 418642(1674568 KB/4 KB) (Using Formula 1)
Total virtual memory pages used by first process = 418642(1674568 KB/4 KB) (Using Formula 1)
Shared memory(SHR)- Shared memory is the memory pages in RAM which shared by more than two process. If two or more processes are using a shared library, then operating system loads that library in RAM(memory pages) and the processes using that library reference the same pages on which that library is loaded.
Total shared memory pages used by the first process = 5389(21556 KB/4 KB) (Using Formula 1)

Nice and informative post related to memory description. Thanks Indresh.
ReplyDelete