Tuesday, 19 November 2013

Virtual Memory - Overview



Virtual memory is a technique that allows the execution of processes which are not completely available in memory.

Provide protection.  
  • One process can’t interfere with another.
  • They operate in their own virtual address spaces.
  • User process can access only User Space.
  • Kernel process can access both User and Kernel Spaces.

Simplify Memory Management
  • Multiple processes resident in main memory.
  • Each process with its own address space.
  • Only “active” code and data is actually in memory – Working Set.

DEFAULT VIRTUAL ADDRESS LAYOUT - Windows
<image has to added>
By default on 32 bit environment, user space is 2GB and kernel space is 2GB.User virtual address space can be increased to 3GB.


To enable the 3GB switch on Windows Vista™ or Later:
Enable:
    Right-click Command Prompt in the Accessories program group of the Start menu.
    Run as administrator.   
    Enter "bcdedit /set IncreaseUserVa 3072"   
    Restart the computer.
Disable:
    Run as administrator.
    Enter "bcdedit /deletevalue IncreaseUserVa   
    Restart the computer.

USER SPACE LAYOUT



Code/Text Segment:
Code segment or simply as text, contains executable instructions.
Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.

Initialized Data Segment / Data segment:
Initialized data segment, usually called simply the Data Segment.
A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.

Note that, data segment is not read-only, since the values of the variables can be altered at run time. This segment can be further classified into.

  • Initialized read-only area.
  • Initialized read-write area.
Global string defined by char s [] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.
Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.

BSS/ Uninitialized Data Segment:
Block start by symbol.
Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing. Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.
Data segment also has global or static data which are initialised to arithematic 0 in the program.

#include <stdio.h>

int gUninitialised;       // BSS Segment
int gInitialzedZero = 0;  // BSS Segment
int gInitialized = 10;    // Data Segment

int main(void){
    static int sUninitialised;        // BSS Segment
    static int sInitialisedZero = 0;  // BSS Segment
    static int sInitialised = 20;     // Data Segment
    return 0;
}

Stack Segment:
The stack area traditionally adjoined the heap area and grew the opposite direction. When the stack pointer met the heap pointer, free memory was exhausted.

The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero. A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack. The set of values pushed for one function call is termed a “stack frame”; A stack frame consists at minimum of a return address.

Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.
The constant reuse of stack regions tends to keep active stack memory in the cpu caches, speeding up access. Each thread in a process gets its own stack.
By default in windows stack size is 1MB and in linux stack size is 4MB. These values can be altered. If the maximum stack size has been reached, we have a stack overflow and the program receives a Segmentation Fault.

Stack Frame:
A call stack is composed of stack frames (also called activation records or activation frames).Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return.
The stack frame at the top of the stack is for the currently executing routine. The stack frame usually includes at least the following items (in push order):
  • the arguments (parameter values) passed to the routine (if any);
  • the return address back to the routine's caller and
  • space for the local variables of the routine (if any).
Heap Segment:
Heap is the segment where dynamic memory allocation usually takes place.
The heap area begins at the end of the BSS segment and grows to larger addresses from there.The Heap area is shared by all shared libraries and dynamically loaded modules in a process. Data stored in the heap must be destroyed after used or it may lead to memory leak. It is used on demand to allocate a block of data for use by the program.

No comments:

Post a Comment