Tuesday 31 December 2013

Linux process memory in detail

Linux process memory in detail


Linux provides detailed information about how a process using the memory. But it could be difficult for a System Admin to understand the difference among the items used by Linux.

There is a program as below:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

void func()
{
    int *parray=NULL;

    if((parray=(int *)malloc(100*sizeof(int)))==NULL)
        printf("failed/n");
    else
    {
        pause();
    }
    return ;
}

int main()
{
        printf("pid: %d\n", getpid());
        func();
        return 0;

}

Run it and the process will pause there.

That’s the output of the 
#top –p [pid]




  • VIRT (VSZ): is the virtual memory for the process. But it does not mean the memory is used by the process now. For example, malloc just allocated some memory but the program hasn’t used it. The kernel will only map the virtual memory to the physical memory when it is really needed.
  • RES(RSS): the physical memory (not swapped)is being used by the program. Including the shared memory (shared lib or shard memory).
  • SHR: shared memory (shared lib or shard memory).  It does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES.


Let’s look it into more detailed output using
#cat /proc/[pid]/status
Just focus on the memory section













  • VmSize: is the virtual memory VIRT (VSZ)
  • VmLck(KB): memory is locked and can’t be swapped. For example, the memory can be locked by munlock function in C.
  • VmHWM: the peak hardware memory usage by the process
  • VmRSS: the physical memory used by the process (RSS)
  • VmData: the program’s data segment. Related with program data
  • VmStk: the program’s stack segment. It contains automatic identifier, register variables, and function call information
  • VmExe: the program’s text segment. contains the executable program code and constant data
  • VmLib: Shared library code size.
  • VmPTE: Page table entries size.


A program memory table can be shown as below (from internet)



No comments:

Post a Comment