Internal Fragmentation in OS
Space lost inside a block a process was given. The allocator rounds a request up to a fixed unit, and the rounded-up part is held by the process but used by nobody.
What Internal Fragmentation Is
In operating system memory management, internal fragmentation is memory that has been given to a process but that the process does not use. It happens whenever the allocator hands out memory in fixed units and rounds a request up to the next unit. The rounded-up part is inside the process's block, so the allocator counts it as used and cannot give it to anyone else until the whole block is freed.
Fixed units are chosen because they make allocation cheap: the allocator never tracks odd sizes, and any free unit fits any request for one unit. The lost space is the price. The other kind of waste, free memory scattered between blocks, is external fragmentation; the two are compared in memory fragmentation and compaction.
- Where: inside an allocated block
- Cause: rounding a request up to a fixed unit
- Freed: only when the whole block is freed
Fixed Partitions: The Textbook Example
Early batch systems such as IBM OS/360 MFT divided memory into fixed partitions at start-up, one process per partition. Take partitions of 100, 500, 200, 300 and 600 KB and requests of 212, 417, 112 and 426 KB, the list used in the memory allocation algorithms lessons.
With first fit, 212 KB takes the 500 KB partition (288 KB lost), 417 KB takes the 600 (183 lost) and 112 KB takes the 200 (88 lost). The three processes hold 1,300 KB and use 741 KB, so 559 KB is internal fragmentation. Then 426 KB waits: the free partitions are 100 and 300 KB, and the 559 KB of waste is locked inside partitions other processes own.
Best fit does better on the same partitions: 212 into 300, 417 into 500, 112 into 200 and 426 into 600. All four fit and 433 KB is lost. A better placement rule reduces the waste but cannot remove it, because a partition's size never matches the request exactly.
- First fit: 559 KB lost, 426 KB waits
- Best fit: 433 KB lost, all four placed
Internal Fragmentation in Paging
Paging gives a process whole pages, so internal fragmentation in paging is confined to the last page of each allocation. With 4 KB pages, a 5 KB request gets two pages, 8 KB, and loses 3 KB. A request that is an exact multiple of 4 KB loses nothing.
On the textbook requests, 212 and 112 KB are multiples of 4 and lose nothing, 417 KB takes 105 pages and loses 3 KB, and 426 KB takes 107 pages and loses 2 KB. The total is 5 KB, against 559 KB with fixed partitions, and nothing waits.
The loss is bounded. At worst a request is one byte past a page edge: 4,097 bytes need two pages and lose 4,095 bytes, one page minus one byte. Request sizes fall at random places within the last page, so on average it is half full and the expected loss is half a page, about 2 KB per allocation (per process or segment, not per byte requested).
- Worst case: page size minus one byte
- Average: half a page per allocation
- Does not grow the longer the system runs
Internal fragmentation, partition by partition
PARTITIONS = [100, 500, 200, 300, 600]
REQUESTS = [212, 417, 112, 426]
PAGE = 4
def fixed_first_fit(partitions, requests):
taken = [False] * len(partitions)
lost = 0
for r in requests:
for i, size in enumerate(partitions):
if not taken[i] and size >= r:
taken[i] = True
lost += size - r
print(f"{r} KB -> {size} KB partition, {size - r} KB lost inside")
break
else:
print(f"{r} KB -> waits")
return lost
def paged(requests, page):
lost = 0
for r in requests:
pages = (r + page - 1) // page
waste = pages * page - r
lost += waste
print(f"{r} KB -> {pages} pages, {waste} KB lost in the last page")
return lost
print("fixed partitions, first fit")
print(f"internal fragmentation: {fixed_first_fit(PARTITIONS, REQUESTS)} KB")
print(f"{PAGE} KB pages")
print(f"internal fragmentation: {paged(REQUESTS, PAGE)} KB")#include <iostream>
#include <vector>
using namespace std;
int fixedFirstFit(const vector<int>& partitions, const vector<int>& requests) {
vector<bool> taken(partitions.size(), false);
int lost = 0;
for (int r : requests) {
bool placed = false;
for (size_t i = 0; i < partitions.size(); i++) {
if (!taken[i] && partitions[i] >= r) {
taken[i] = true;
lost += partitions[i] - r;
cout << r << " KB -> " << partitions[i] << " KB partition, "
<< partitions[i] - r << " KB lost inside\n";
placed = true;
break;
}
}
if (!placed) cout << r << " KB -> waits\n";
}
return lost;
}
int paged(const vector<int>& requests, int page) {
int lost = 0;
for (int r : requests) {
int pages = (r + page - 1) / page;
int waste = pages * page - r;
lost += waste;
cout << r << " KB -> " << pages << " pages, " << waste << " KB lost in the last page\n";
}
return lost;
}
int main() {
vector<int> partitions = {100, 500, 200, 300, 600};
vector<int> requests = {212, 417, 112, 426};
int page = 4;
cout << "fixed partitions, first fit\n";
int fixedLost = fixedFirstFit(partitions, requests);
cout << "internal fragmentation: " << fixedLost << " KB\n";
cout << page << " KB pages\n";
int pagedLost = paged(requests, page);
cout << "internal fragmentation: " << pagedLost << " KB\n";
}public class Main {
static int fixedFirstFit(int[] partitions, int[] requests) {
boolean[] taken = new boolean[partitions.length];
int lost = 0;
for (int r : requests) {
boolean placed = false;
for (int i = 0; i < partitions.length; i++) {
if (!taken[i] && partitions[i] >= r) {
taken[i] = true;
lost += partitions[i] - r;
System.out.println(r + " KB -> " + partitions[i] + " KB partition, "
+ (partitions[i] - r) + " KB lost inside");
placed = true;
break;
}
}
if (!placed) System.out.println(r + " KB -> waits");
}
return lost;
}
static int paged(int[] requests, int page) {
int lost = 0;
for (int r : requests) {
int pages = (r + page - 1) / page;
int waste = pages * page - r;
lost += waste;
System.out.println(r + " KB -> " + pages + " pages, " + waste + " KB lost in the last page");
}
return lost;
}
public static void main(String[] args) {
int[] partitions = {100, 500, 200, 300, 600};
int[] requests = {212, 417, 112, 426};
int page = 4;
System.out.println("fixed partitions, first fit");
System.out.println("internal fragmentation: " + fixedFirstFit(partitions, requests) + " KB");
System.out.println(page + " KB pages");
System.out.println("internal fragmentation: " + paged(requests, page) + " KB");
}
}Step through it
Running on partitions 100, 500, 200, 300, 600 KB · requests 212, 417, 112, 426 KB
Page Size and Other Allocators
Page size sets the size of the loss. Smaller pages lose less in the last page but need more page table entries, and a TLB of fixed size then covers less memory. Larger pages do the reverse. With 2 MB huge pages the four textbook requests lose 7,025 KB while using 1,167 KB, which is why huge pages are used for large, long-lived mappings such as a database buffer pool rather than for every allocation.
The same rounding happens in allocators that never touch pages. The buddy system hands out powers of two, so a 33 KB request takes a 64 KB block and loses 31 KB. glibc's malloc rounds every request up to its chunk size, so even malloc(1) on a 64-bit system takes a 32-byte chunk. Slab caches round each object up to the slot size of its cache.
- 4 KB pages: about 2 KB lost per allocation
- 2 MB pages: about 1 MB lost per allocation
- Buddy system: up to almost half the block
How to Reduce It
- Use a smaller unit where the bookkeeping cost allows it
- Space size classes closely, as jemalloc and tcmalloc do, so the rounding stays within a known percentage
- Give objects of one size their own slab cache, so slots match the objects
- Reserve huge pages for large mappings that fill them
Every fix narrows the gap between what is asked for and what is handed out. Variable partitions remove internal fragmentation completely, because each process gets exactly its size, but they bring back external fragmentation instead.
- Exact-size allocation removes it but brings back external fragmentation