When an executable application is loaded into main memory it becomes a 'process'.
Processes require memory to run. The operating system sets aside memory for the process to use. This is called 'segmentation'. Segments are not a fixed size; they will be as large as is necessary to hold the process.
Memory segmentation is the allocation of segments or sections of memory to allow a process to run
The memory segment given to the process is further divided into several sections - the 'stack segment', 'data segment', 'code segment' and 'free memory'.
These smaller segments don't have to be physically next to one another because each process is tracked by the operating system.
The 'code segment' is where the program instructions for the process reside.
The 'data segment' is a memory space allocated for all the variables that the program will use.
The 'stack segment' stores memory addresses whenever a process begins to run a subroutine:
- Whenever an instruction is interrupted by a subroutine, the program notes the address of where it left off.
- This address is kept in the stack while the subroutine runs.
- When the subroutine is complete, the program returns to the address stored in the stack.
The more subroutines are running, the more memory is needed in the stack to store addresses. Memory is freed up again when the subroutines are completed. So the stack is continually getting bigger and smaller while the program runs.
The 'free memory' is allocated memory for the stack to grow and shrink as necessary.