Thursday, April 12, 2012

Virtual Address Space and Virtual Memory


Each process has it's own Virtual Address Space which is mapped to physical memory(RAM) by the operating system.

A virtual address does not represent the actual physical location of an object in memory; instead, the system maintains a page table for each process, which is an internal data structure used to translate virtual addresses into their corresponding physical addresses in RAM. Each time a process references an address, the system translates the virtual address to a physical address.

Each time an application is run on an operating system (OS), the OS creates a new process and a new Virtual Address Space for this process.The virtual address space for each process is private and cannot be accessed by other processes unless it is shared. Hence, a process memory is Virtual Address Space memory and not physical memory.

The virtual address space for 32-bit system is 4 gigabytes (GB) in size (2^32 - 1). So when a new process is run on a 32-bit OS, a 4GB virtual address space is allocated for this process.

Virtual Memory of a process is a combination  of  RAM space used by the process plus Disk space used by the process. Swap space used by a process is thus part of the virtual memory of the process.

The kernel (on the x86 architecture, in the default configuration) splits the 4-GB virtual address space between user-space and the kernel space. The  4 GB virtual address space of a process is split in two parts: 3 GB and 1 GB. The lower 3 GB of the process virtual address space is accessible as the user-space virtual addresses and the upper 1 GB space is reserved for the kernel virtual addresses. This is true for all processes.

The virtual memory or memory map of a process includes atleast the following areas
  • Text - An area for program's executable code - Set of instructions from the compiler-generated executable file.
  • An area for the initialized data of the program (data) -  Global variables, constants, static variables from the program.
  • An area for the uninitialized data of the program (BSS)Uninitialized variables. These are not part of the executable file and their initial value is set to zeros.
  • Three areas (text, data, BSS) for each shared library
  • The stack area -  Used by the program for variables and storage. It grows and shrinks in size depending on what routines are called and what their stack space requirements are.
  • hole - This is the address space that is unallocated and unused. It does not tie up physical memory. For most processes, this is the largest portion of the virtual memory for the process

The areas of a process can be found from /proc/<pid>/maps

# cat /proc/1/maps
0012e000-0012f000 r-xp 00000000 00:00 0          [vdso]
00a51000-00a5c000 r-xp 00000000 fd:01 1353       /lib/libnss_files-2.14.so
00a5c000-00a5d000 r--p 0000a000 fd:01 1353       /lib/libnss_files-2.14.so
00a5d000-00a5e000 rw-p 0000b000 fd:01 1353       /lib/libnss_files-2.14.so
08048000-08108000 r-xp 00000000 fd:01 2254       /bin/systemd
08108000-0810d000 rw-p 000bf000 fd:01 2254       /bin/systemd
08b67000-096ab000 rw-p 00000000 00:00 0          [heap]
4108f000-4109c000 r-xp 00000000 fd:01 1313       /lib/libpam.so.0.83.1
4109c000-4109d000 rw-p 0000c000 fd:01 1313       /lib/libpam.so.0.83.1
4aa94000-4aab1000 r-xp 00000000 fd:01 1325       /lib/ld-2.14.so
4aab1000-4aab2000 r--p 0001d000 fd:01 1325       /lib/ld-2.14.so
4aab2000-4aab3000 rw-p 0001e000 fd:01 1325       /lib/ld-2.14.so
4aab5000-4ac3a000 r-xp 00000000 fd:01 1327       /lib/libc-2.14.so
4ac3a000-4ac3b000 ---p 00185000 fd:01 1327       /lib/libc-2.14.so
4ac3b000-4ac3d000 r--p 00185000 fd:01 1327       /lib/libc-2.14.so
4ac3d000-4ac3e000 rw-p 00187000 fd:01 1327       /lib/libc-2.14.so
4ac3e000-4ac41000 rw-p 00000000 00:00 0
4ac43000-4ac59000 r-xp 00000000 fd:01 1336       /lib/libpthread-2.14.so
4ac59000-4ac5a000 r--p 00015000 fd:01 1336       /lib/libpthread-2.14.so
4ac5a000-4ac5b000 rw-p 00016000 fd:01 1336       /lib/libpthread-2.14.so
4ac5b000-4ac5d000 rw-p 00000000 00:00 0
4ac5f000-4ac62000 r-xp 00000000 fd:01 1360       /lib/libdl-2.14.so
4ac62000-4ac63000 r--p 00002000 fd:01 1360       /lib/libdl-2.14.so
4ac63000-4ac64000 rw-p 00003000 fd:01 1360       /lib/libdl-2.14.so
4ac66000-4ac6d000 r-xp 00000000 fd:01 1346       /lib/librt-2.14.so

(Address range in virtual memory) (Permissions) (Offset) (Device#) (Inode) (File name)
  • Address range in virtual memory - The beginning and ending virtual addresses for this memory area.
  • Permissions - A bit mask with the memory area's read, write, and execute permissions. This field describes what the process is allowed to do with pages belonging to the area. The last character in the field is either p for "private" or s for "shared."
  • Offset - Where the memory area begins in the file that it is mapped to. An offset of 0 means that the beginning of the memory area corresponds to the beginning of the file.
  • Device # - It consist of major & minor numbers of the device holding the file that has been mapped. Confusingly, for device mappings, the major and minor numbers refer to the disk partition holding the device special file that was opened by the user, and not the device itself.
  • Inode - The inode number of the mapped file.
  • Filename - The name of the file (usually an executable image) that has been mapped.

No comments:

Post a Comment