Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is there internal fragmentation in paging and no external fragmentation?

Please explain it nicely. Don't just write definition. Also explain what it does and how is it different from segmentation.

like image 904
MAD CODER Avatar asked Sep 23 '15 16:09

MAD CODER


2 Answers

Fragmentation needs to be considered with memory allocation techniques. Paging is basically not a memory allocation technique, but rather a means of providing virtual address spaces.

Considering the comparison with segmentation, what you're probably asking about is the difference between a memory allocation technique using fixed size blocks (like the pages of paging, assuming 4KB page size here) and a technique using variable size blocks (like the segments used for segmentation).

Now, assume that you directly use the page allocation interface to implement memory management, that is you have two functions for dealing with memory:

  • alloc_page, which allocates a single page and returns a pointer to the beginning of the newly available address space, and
  • free_page, which frees a single, allocated page.

Now suppose all of your currently available virtual memory is used, but you need to store 1 additional byte. You call alloc_page and get a 4KB block of memory. You only use 1 byte of that huge block, but also the other 4095 bytes are, from the perspective of the allocator, used. If this happens multiple times eventually all pages will be allocated, so further calls to alloc_page will fail. Even if you just need another additional byte (which could be one of the 4095 that got wasted above) the allocator will tell you that you're out of memory. This is internal fragmentation.

If, on the other hand, you would use variable sized blocks (like in segmentation), then you're vulnerable to external fragmentation: Suppose you manage 6 bytes of memory (F means "free"):

FFFFFF

You first allocate 3 bytes for a, then 1 for b and finally 2 bytes for c:

aaabcc

Now you free both a and c, leaving only b allocated:

FFFbFF

You now have 5 bytes of unused memory, but if you try to allocate a block of 4 bytes (which is less than the available memory) the allocation will fail due to the unfavorable placement of the memory for b. This is external fragmentation.

Now, if you extend your page allocator to be able to allocate multiple pages and add alloc_multiple_pages, you have to deal with both internal and external fragmentation.

like image 168
Daniel Jour Avatar answered Oct 27 '22 11:10

Daniel Jour


There is no external fragmentation in paging but internal fragmentation exists. First, we need to understand what is external fragmentation. External fragmentation occurs when we have a memory to accommodate a process but it's not continuous. How does it not occur in paging? Paging divides virtual memory or all processes into equal-sized pages and physical memory into fixed size frames. So you are typically fixing equal size blocks called pages into equal block shaped spaces called frames! Try to visualize and conclude that there can never be external fragmentation.

In the case of segmentation, we divide virtual addresses into different sized blocks that is why there may be the case some blocks in main memory must stick together or compact to make space for the new process! I hope it helps!

like image 30
Akash Jain Avatar answered Oct 27 '22 11:10

Akash Jain