Memory allocation is an action of assigning the physical or the virtual memory address space to a process (its instructions and data). The two fundamental methods of memory allocation are static and dynamic memory allocation.
The static memory allocation method assigns the memory to a process, before its execution. On the other hand, the dynamic memory allocation method assigns the memory to a process, during its execution.
In this section, we will be discussing what is memory allocation, its types (static and dynamic memory allocation) along with their advantages and disadvantages. So let us start.
Content: Static and Dynamic Memory Allocation
- Memory Allocation
- Types of Memory Allocation
- Advantages and Disadvantages of Static and Dynamic Memory Allocation
- Key Takeaways
Memory Allocation
To get a process executed it must be first placed in the memory. Assigning space to a process in memory is called memory allocation. Memory allocation is a general aspect of the term binding.
Let us understand binding with the help of an example. Suppose, there is an entity in a program, with a set of attributes. Now, a variable of this entity will have values for this set of attributes. For storing these values, we must have memory allotted to these attributes.
So, the act of assigning the memory address to the attribute of the variable is called memory allocation. And the act of specifying/binding the values to the attributes of the variable is called binding. This action of binding must be performed before the variable is used during the execution of the program.
We have two types of memory allocation or we can say two methods of binding, static and dynamic binding.
Types of Memory Allocation
1. Static Memory Allocation
Static memory allocation is performed when the compiler compiles the program and generates object files. The linker merges all these object files and creates a single executable file. The loader loads this single executable file in the main memory, for execution. In static memory allocation, the size of the data required by the process must be known before the execution of the process initiates.
If the data sizes are not known before the execution of the process, then they have to be guessed. If the data size guessed is larger than the required, then it leads to wastage of memory. If the guessed size is smaller, then it leads to inappropriate execution of the process.
The static memory allocation method does not need any memory allocation operation during the execution of the process. All the memory allocation operation required for the process is done before the execution of the process has started. So, it leads to faster execution of a process.
Static memory allocation provides more efficiency when compared to dynamic memory allocation.
2. Dynamic Memory Allocation
Dynamic memory allocation is performed while the program is in execution. Here, the memory is allocated to the entities of the program when they are to be used for the first time while the program is running.
The actual size, of the data required, is known at the run time so, it allocates the exact memory space to the program thereby, reducing the memory wastage.
Dynamic memory allocation provides flexibility to the execution of the program. As it can decide what amount of memory space will be required by the program. If the program is large enough then a dynamic memory allocation is performed on the different parts of the program, which is to be used currently. This reduces memory wastage and improves the performance of the system.
Allocating memory dynamically creates an overhead over the system. Some allocation operations are performed repeatedly during the program execution creating more overheads, leading in slow execution of the program.
Dynamic memory allocation does not require special support from the operating system. It is the responsibility of the programmer to design the program in a way to take advantage of dynamic memory allocation method.
Thus the dynamic memory allocation is flexible but slower than static memory allocation.
Advantages of static and dynamic memory allocation
Static Memory Allocation
- Static memory allocation provides an efficient way of assigning the memory to a process.
- All the memory assigning operations are done before the execution starts. So, there are no overheads of memory allocation operations at the time of execution of the program.
- Static memory allocation provides faster execution, as at the time of execution it doesn’t have to waste time in allocation memory to the program.
Dynamic Memory Allocation
- Dynamic memory allocation provides a flexible way of assigning the memory to a process.
- Dynamic memory allocation reduces the memory wastage as it assigns memory to a process during the execution of that program. So, it is aware of the exact memory size required by the program.
- If the program is large then the dynamic memory allocation is performed on the different parts of the program. Memory is assigned to the part of a program that is currently in use. This also reduces memory wastage and indeed improves system performance.
Disadvantages of static and dynamic memory allocation
Static Memory Allocation
- In static memory allocation, the system is unaware of the memory requirement of the program. So, it has to guess the memory required for the program.
- Static memory allocation leads to memory wastage. As it estimates the size of memory required by the program. So, if the estimated size is larger, it will lead to memory wastage else if the estimated size is smaller, then the program will execute inappropriately.
Dynamic Memory allocation
- Dynamic memory allocation method has an overhead of assigning the memory to a process during the time of its execution.
- Sometimes the memory allocation actions are repeated several times during the execution of the program which leads to more overheads.
- The overheads of memory allocation at the time of its execution slowdowns the execution to some extent.
Key Takeaways
- Memory allocation specifies the memory address to a program or a process.
- Memory allocation has two methods static memory allocation and dynamic memory allocation.
- Static memory allocation provides efficiency as it assigns the memory to a process before its execution has started. So it doesn’t have any overhead of memory allocation operation during the execution of the program which leads to faster execution of the program.
- In static memory allocation, the required memory size must be known prior to the execution of the program.
- Static memory allocation assigns the assumed amount of memory space to a process as it is unaware of the amount of memory required by the program. This leads to the wastage of memory.
- Dynamic memory allocation is performed during the time of execution of a program. So it allocates the exact amount of memory to the program avoiding memory wastage.
- Dynamic memory allocation has the overheads of memory allocation operation during the execution of the program which slowdowns the execution of the program.
- Dynamic memory allocation provides flexibility during memory allocation, as if the program is large enough then it performs memory allocation operations on different parts of the programs and reduces memory wastage.
The operating system can obtain the best mix of static and dynamic memory allocation to get an efficient and flexible execution.
Cindy says
Hi, very nice website, cheers!
Megh Raj says
The contents are clear and straight to the topic. Good work and hope to see more such rich content. in the coming days.
Beli says
Hi Neha T,
You have explained everything very well.
Tuan Le says
Very nice content