Memory Fragmentation

If objects are not managed correctly then memory fragmentation can be a major issue, especially in games programming. Using an object pool helps to alleviate fragmentation.

Memory fragmentation is when the free space on the heap is broken into small pieces. This means that the total available space may be large, but due to the current location of all data on the heap there isn’t a single continuous block that is large enough to accommodate a new object.png_1

The above diagram shows the heap with 3 objects. Now lets say that the first and last object have been used and they are now destroyed. However, our program now requires another blue object. As you can see, the total free space is large enough to accommodate another object but it is split on either side of the existing object. This means that the program would need to wait until there is a single continuous block large enough.

If the same program had an Object Pool, this issue would not occur providing that you initially created enough objects. This would mean that all 4 of the required objects would be created on startup. The object pool would manage the objects instead of destroying them so there would be no gaps in the heap and the objects would keep their location on the heap.

Leave a comment