Memory Management in Operating System
Memory is the basic element needed for any software feature or application.
In memory management at a high level we can classify the memory into two types Stack memory and Heap memory.
Stack Memory :
Within a function or method we have several local or global parameters. This memory resides in RAM. All local parameters are stored in a stack memory. Now the obvious question is WHY ?
We already learned about the stack data structure which uses the concept of First In Last Out(FILO).
Let us see the practical application of stack memory
Consider the functions in the picture below
Stage 1 : Calling function B
Stage 2 : Calling function C
We can clearly see the reusability of memory space using stack data structure in memory.
The memory space used for local parameters of function B is used for function C or any other function called down the line.
The address of the stack frame decreases as we add new stack frames as can be seen in the image above.
Heap Memory :
Data stored in this memory is accessible through out the life of the program or application. This memory resides in RAM.Global, static and dynamic data is stored in heap memory.
This is completely different from the heap data structure which is used in sorting operation.
Heap memory is allocated by using functions called malloc and new. It can be deallocated by using free and delete respectively.
Allocating memory space in heap :
As can be seen in the image above we use new key word to allocate memory in a heap memory.
If we do not free or delete the memory once we are done using the memory space it will lead to memory leak.
No comments:
Post a Comment