# A Metric for Memory Fragmentation
Wed
06
Apr 2022
In this article, I would like to discuss the problem of memory fragmentation and propose a formula for calculating a metric telling how badly the memory is fragmented.
Problem statement
The problem can be stated like this:
- We consider a linear, addressable memory with a limited capacity.
- We develop some code that allocates and frees parts of this memory by request from the user (a program that is using our library).
- Allocations can be of arbitrary size, varying from single bytes to megabytes or even gigabytes.
- "Allocate" and "free" requests can happen in random order.
- Allocations cannot overlap. Each one must have its own memory region.
- Each allocation must be placed in a continuous region of memory. It cannot be made of multiple disjoint regions.
- Once created, an allocation cannot be moved to a different place. Its region must be considered as occupied until the allocation is freed.
So it is a standard memory allocation situation. Now, I will explain what do I mean by fragmentation. Fragmentation, for this article, is an unwanted situation where free memory is spread across many small regions in between allocations, as opposed to a single large one. We want to measure it and preferably avoid it because:
- It increases a chance that some future allocation couldn't be made, even with sufficient total amount of free memory, because no large enough free region could be found to satisfy the request.
- When talking about the entire memory available to a program, this can lead to program failure, crash, or some undefined behavior, depending on how well the program handles memory allocation failures.
- When talking about acquiring large blocks of memory from the operating system (e.g. with WinAPI function
VirtualAlloc) and sub-allocating them for the user's allocation requests, high fragmentation my require allocating another block to satisfy the request, making the program using more system memory than really needed.
- Similarly, when most allocations are freed, our code may not release memory blocks to the system, as there are few small allocations spread across them.
A solution to this problem is to perform defragmentation - an operation that moves the allocations to arrange them next to each other. This may require user involvement, as pointers to the allocations will change then. It may also be a time-consuming operation to calculate better places for the allocations and then to copy all their data. It is thus desirable to measure the fragmentation to decide when to perform the defragmentation operation.
Comments |
#gpu #algorithms #optimization
Share