Memory Management Fundamentals
Memory management is the process of controlling and coordinating the way a software application access computer memory.
When a software runs on a target Operating system on a computer it needs access to the computers RAM(Random-access memory) to:
- load its own bytecode that needs to be executed
- store the data values and data structures used by the program that is executed
- load any run-time systems that are required for the program to execute
When a software program uses memory there are two regions of memory they use, apart from the space used to load the bytecode, Stack and Heap memory.
The stack is used for static memory allocation:
- Due to this nature (LIFO), the process of storing and retrieving data from the stack is very fast as there is no lookup required, you just store and retrieve data from the topmost block on it.
- But this means any data that is stored on the stack has to be finite and static(The size of the data is known at compile-time).
- This is where the execution data of the functions are stored as stack frames(So, this is the actual execution stack). Each frame is a block of space where the data required for that function is stored.
- For example, every time a function declares a new variable, it is “pushed” onto the topmost block in the stack. Then every time a function exits, the topmost block is cleared, thus all of the variables pushed onto the stack by that function, are cleared. These can be determined at compile time due to the static nature of the data stored here.
- Multi-threaded applications can have a stack per thread.
- Memory management of the stack is simple and straightforward and is done by the OS.
- Typical data that are stored on stack are local variables(value types or primitives, primitive constants), pointers and function frames.
- This is where you would encounter stack overflow errors as the size of the stack is limited compared to the Heap.
- There is a limit on the size of value that can be stored on the Stack for most languages.
- Stack is single threaded, only one thread can access the stack at a time.
Heap is used for dynamic memory allocation and unlike stack, the program needs to look up the data in heap using pointers:
- It is slower than stack as the process of looking up data is more involved but it can store more data than the stack. This means data with dynamic size can be stored here.
- Heap is also slow due to the fact that it might request memory from OS which require a system call.
- What happen is when we need memory which program don’t have a syscall is made and instead of providing a required memory OS provide with chunk of memory so that further syscalls can be reduced but if memory is not available syscall is made.
- Stack is predictable while heap is not as in stack only push and pop can happen on top of it while in Heap it can happen from any point making it slow.