external fragmentation in os with code examples

External fragmentation in operating systems is a memory management problem that happens when there is enough memory space available to fulfill a request from a process, but the space is split into several small fragments that cannot be combined to form a contiguous block of memory that can meet the request. This type of fragmentation will ultimately result in inefficient memory utilization, slowdowns, and decreased system performance. External fragmentation can occur in many different types of operating systems, but for the purposes of this article, we will focus mainly on examples in the Windows operating system.

What is External Fragmentation?

External fragmentation happens when a process needs to be allocated memory from the main memory pool, but the memory space is available in pieces that are scattered throughout the system. This results in wasted memory until the operating system can free up enough space to create a block that can accommodate the process's request. The space that exists between these fragments is called the holes and the free space within a block is called internal fragmentation.

External fragmentation in Operating System is the non-availability of contiguous blocks of memory to a new process or a new piece of data, despite having the required amount of free space available.

External fragmentation occurs when a process is allocated memory in non-contiguous blocks, which then gets freed up over time, leaving only a small piece of space available in each block. Over time, these small pieces start to add up, resulting in a situation where there is enough space to hold a new process or data block, but the space is not contiguous, and hence cannot be used.

External fragmentation is a common problem when dealing with dynamic memory allocation, where a memory pool is allocated and deallocated on a regular basis. Dynamic allocation can result in memory blocks being freed up at different times, leading to fragmentation over time.

External Fragmentation Examples using C

Now let's look at some examples of external fragmentation in C. In C, the heap is a common place for external fragmentation to occur. The heap is where dynamic memory allocation occurs and where new memory is requested and allocated.

1. Fragmentation due to Memory Allocation and Deallocation

#include <stdio.h>
#include <stdlib.h>

int main()
{
int p1 = (int) malloc(sizeof(int));
int p2 = (int) malloc(sizeof(int));
int p3 = (int) malloc(sizeof(int));
int p4 = (int) malloc(sizeof(int));

free(p2); 
free(p3); 

int *p5 = (int*) malloc(3*sizeof(int)); 

if (p5 == NULL) 
{ 
    printf("Memory could not be allocated

");
exit(0);
}

*p1 = 1; 
*p4 = 4; 

printf("%d %d %d %d", *p1, *p2, *p3, *p4); 

return 0; 

}

In this example, we have four pointers (p1, p2, p3, p4) that are assigned memory using malloc(). Pointers p2 and p3 are then freed up, leaving two small, non-contiguous pieces of memory. When p5 is requested and allocated memory, it is not able to find a contiguous block of memory, even though there is enough space available.

2. Fragmentation due to insertion and deletion

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node* next;
};

int main()
{
struct node* p1 = (struct node*) malloc(sizeof(struct node));
struct node* p2 = (struct node*) malloc(sizeof(struct node));
struct node* p3 = (struct node*) malloc(sizeof(struct node));

p1->data = 1; 
p1->next = p2; 

p2->data = 2; 
p2->next = p3; 

p3->data = 3; 
p3->next = NULL; 

free(p2); 

struct node* p4 = (struct node*) malloc(sizeof(struct node)); 

if (p4 == NULL) 
{ 
    printf("Memory could not be allocated

");
exit(0);
}

p4->data = 4; 
p4->next = NULL; 

printf("%d %d %d %d", p1->data, p2->data, p3->data, p4->data); 

return 0; 

}

In this example, we have a linked list consisting of three nodes (p1, p2, p3), with p1 pointing to p2, p2 pointing to p3, and p3 pointing to null. After p2 is freed up, a new node (p4) is inserted, but there is not enough contiguous memory to store it. This results in external fragmentation.

Conclusion

External fragmentation is a memory management problem that can have a major impact on system performance. Memory allocation and deallocation, as well as insertions and deletions, can lead to external fragmentation. When external fragmentation occurs, the operating system will have to find a way to combine the scattered pieces of memory into a contiguous block so that the blocked can be used by a new process or data.

To reduce external fragmentation, the usage of a defragmentation algorithm like malloc_trim() can be applied, which will automatically combine small fragments of free memory space into more significant chunks and minimize external fragmentation.

Overall, it is important for developers and system administrators to understand external fragmentation and take steps to monitor and minimize it. By doing so, they can reduce system slowdown, increase performance, and prevent errors caused by insufficient system resources.

Sure. Let's dive deeper into the topic of external fragmentation in operating systems and look at various causes, effects, and solutions of this problem.

Causes of External Fragmentation

External fragmentation can happen due to various reasons, such as:

  1. Dynamic Memory Allocation: External fragmentation is a common byproduct of dynamic memory allocation. When memory is allocated dynamically, free memory blocks of different sizes are created, which can create small chunks of free memory that cannot be combined to create a contiguous block.

  2. Laying Out Large Data Structures: Large data structures, such as arrays, lists, and trees, can also cause external fragmentation. This is because data structures require a contiguous block of memory, and if enough space is not available in one place, the operating system would split it over several locations.

  3. Paging: Paging algorithms, which allow the operating system to map logical memory addresses to physical memory addresses, can also lead to external fragmentation. Paging requires large contiguous blocks of memory, and if they are not available, the operating system must split smaller blocks to create the necessary contiguous blocks of memory.

Effects of External Fragmentation

External fragmentation can have many negative effects on the system performance, such as:

  1. Increased Memory Usage: When the system cannot find contiguous blocks of memory, it increases the amount of memory required by the OS.

  2. Slower Performance: Fragmentation leads to the creation of small, unused memory blocks that can cause performance degradation since the OS must waste additional time managing these unnecessary blocks.

  3. System Crashes: When the system becomes over-burdened with small memory fragments, it can crash.

Solutions to External Fragmentation

To prevent external fragmentation, a few possible solutions are:

  1. Fixed Partition Allocation: In fixed partition memory allocation, the memory is partitioned into fixed-size blocks, and these blocks are assigned to different processes. This solution is not as efficient as dynamic memory allocation but it minimizes fragmentation.

  2. Compaction techniques: This technique is useful when there is enough memory available, and the operating system moves memory blocks so that they are contiguous.

  3. Paginating techniques: Instead of allocating contiguous memory blocks, the operating systems map the logical address space of a process onto physical address space of RAM.

  4. Dynamic Memory Allocation: Dynamic memory allocation can be used to minimize external fragmentation, and operating systems use techniques like best-fit and first-fit to allocate only as much memory as necessary for the running process.

In conclusion, external fragmentation is one of the biggest issues in operating systems and can cause a range of problems with system performance and stability. By analyzing the cause of external fragmentation and choosing the right solutions, developers and system administrators can mitigate the effects of resource fragmentation and improve system performance and stability.

Popular questions

  1. What is external fragmentation, and how does it occur?

External fragmentation is a memory management problem within an operating system that occurs when there are enough memory spaces available to allocate to a process, but the space is scattered throughout the system in small fragments that cannot be combined to meet the process's request. It happens primarily due to dynamic memory allocation and laying out large data structures that create small free memory blocks, leading to fragmentation of free memory.

  1. How does external fragmentation affect system performance?

External fragmentation can have a significant negative impact on system performance, including increased memory usage, slower performance, and, in some cases, system crashes. The presence of small, unused memory blocks causes performance degradation due to additional management time required, and it increases the amount of memory required by the operating system.

  1. What are some examples of programming languages where external fragmentation is a concern?

External fragmentation is a concern in a variety of programming languages and environments, such as C, C++, Java, and operating systems like Windows, Mac, and Linux.

  1. How can external fragmentation be prevented or minimized?

Several techniques can be applied to prevent or minimize external fragmentation. Fixed partition allocation, paging, and dynamic memory allocation are commonly used solutions. In fixed partition allocation, memory is partitioned into fixed-size blocks, and these blocks are assigned to different processes. In paging, the logical memory address of a process is mapped to the physical address space of RAM, eliminating the need for contiguous memory blocks. Dynamic memory allocation allocates only as much memory as necessary for the running process, minimizing fragmentation.

  1. Can you give an example of external fragmentation in C?

An example of external fragmentation in C can be seen in dynamic memory allocation where free memory blocks of different sizes are created. This creates small fragments of free memory that cannot be combined into a contiguous block. As a result, a new process requesting a certain size of memory may fail to be allocated appropriately despite the memory space being available. This is external fragmentation that could lead to performance degradation and a possible system crash.

Tag

MemoryFragmentation

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top