Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does a process look like in memory?
A process is a program loaded into memory and executing. When a program is created, it exists as bytes stored on the hard disk as a passive entity. The program becomes an active process when loaded into memory ? either by double-clicking in Windows or entering the executable name on the command line (e.g., a.out or prog.exe).
A process in memory consists of several distinct segments, each serving a specific purpose during program execution.
Text Section
The Text Section contains the executable instructions of the program. It includes the program code, constants, and macros. This segment is read-only to prevent accidental modification of instructions and is shareable so multiple processes can use the same code simultaneously.
Data Section
The Data Section stores global and static variables that are initialized by the programmer before program execution. Unlike the text section, this segment is writable since variable values can change during runtime.
Example in C −
#include <stdio.h>
int b = 10; // stored in data section
int main() {
static int a = 5; // stored in data section
return 0;
}
Heap Section
The Heap is used for dynamic memory allocation when variable sizes cannot be determined at compile time. Memory is allocated and deallocated using system calls like malloc(), calloc(), free(), and delete. The heap grows upward toward higher memory addresses.
Stack Section
The Stack contains temporary data including function parameters, return addresses, and local variables. On x86 architecture, it grows downward toward lower addresses. The stack and heap grow in opposite directions to avoid overlapping and maximize available memory.
A stack pointer register tracks the top of the stack, indicating how much stack area the current process is using. It is modified each time a value is pushed onto or popped from the stack. If the stack pointer meets the heap pointer, available free memory is depleted.
Memory Management
| Section | Purpose | Access | Growth |
|---|---|---|---|
| Text | Executable code | Read-only | Fixed size |
| Data | Global/static variables | Read-write | Fixed size |
| Heap | Dynamic allocation | Read-write | Grows up |
| Stack | Function calls | Read-write | Grows down |
Conclusion
A process in memory is organized into four main sections ? text, data, heap, and stack ? each serving specific purposes during program execution. This memory layout ensures efficient organization, protection of code, and flexible allocation of both static and dynamic data.
