Skip to main content

Virtual Memory(VIRT), Shared memory(SHR) and Resident memory(RES) explained

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 


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.



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)



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)   


Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Calculating CPU utilisation of a process inside C program?

The top program provides a dynamic real-time view of a running system.  It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. Mostly "top" command is used for checking CPU utilisation of processes, which helps user to check which processes are CPU intensive. By default " top " command refreshes output in interval of 3 seconds. So the percentage of CPU utilisation shown is the average CPU utilisation of a process in last 3 seconds. Check the below pictures, these are the pictures captured in 3 second interval in which top refreshes its output. Note that the time is marked in red and percentage CPU utilisation field is marked in green. top output at 15:52:22 shows percentage of CPU utilisation of processes between 15:52:19 to 15:52:22. Output refreshes after 3 seconds Suppose that user wants to see percentage CPU utilization of processes in 5 min then it can run top command...

Sticky bits in linux

Consider a scenario where you create a Linux directory that can be used by all the users of the Linux system for creating files. Users can create, delete or rename files according to their need in this directory. If you think why would such a directory be created? There exists, for example, /tmp directory in the Linux system that can be used by different Linux users to create temporary files. Now, what if a user accidentally or deliberately deletes (or rename) a file created by some other user in this directory? So to avoid these kind of issues, the sticky bit concept is used. A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user. Given below is the command to set sticky bit on on a file or folder: bash-4.2$ chmod +t accessibleByAll/ bash-4.2$ ls -ld accessibleByAll/ drwxrwxrw t 2 indresh indresh ...