Programs and Memory
In computing, memory refers to the computer hardware devices involved to store information for immediate use in a computer (primary storage).
Computer memory operates at a high speed, such as random-access memory (RAM) with lower capacities. Storage provides slow-to-access program and data storage but offers higher capacities.
Random-access memory (RAM) is a form of computer data storage which stores frequently used program instructions to increase the general speed of a system.
If needed, contents of the computer memory can be transferred to secondary storage, through a memory management technique called "virtual memory".
Virtual memory is a system where all physical memory is controlled by the operating system. When a program needs memory, it requests it from the operating system. The operating system then decides what physical location to place the memory in. The operating system with the help of a hardware component called MMU (memory management unit), maps those virtual memory addresses with real memory address in the physical memory. This functionality is achieved by breaking the physical memory into segments which we refer to as Pages.
When a program is being executed, it is called a process. Typically a process has 5 different areas of memory allocated
1. Code - text segment : Contains the frequently executed code.
2. Initialized data – data segment : Contains Initialized global variables (including pointer variables), Initialized constant global variables, Initialized local static variables.
3. Uninitialized data – bss segment : Contains Uninitialized global variables (including pointer variables), Uninitialized constant global variables, Uninitialized local static variables.
Stack and Heap:
Both are stored in computer RAM and typically occupy the virtual memory above the text and data segments.
Variables created on the stack will go out of scope and automatically get deallocated. Stack is also called "automatic storage class". Therefore ,Stack stores local data/variables, return addresses. Variables on the heap must be handled manually. They are created with "new" and freed with "delete" keywords in c++. ("malloc" and "free" in C)
Stack variables are faster to allocate in comparison to variables on the heap. The reason for this is since stack is implemented with an actual stack data structure, making it easy to keep pointer to next location because all free memory is always contiguous. No list needs to be maintained of all the segments of free memory, just a single pointer to the current top of the stack.
Stack is vulnerable to have a stack overflow when too much of the stack is used. (like recursion, very large allocations) Heaps can have fragmentation when there are a lot of allocations and deallocations. Heap is vulnerable to memory leaks when memory is not properly deallocated.
Stack usually has a maximum size already determined when your program starts. Stack is used if we know exactly how much data we need to allocate before compile time and it is not too big. Heap is used when you don't know exactly how much data you will need at run time or if you need to allocate a lot of data.