3 Memory allocations and performance
This chapter covers
- Memory Fragmentation.
- Avoiding garbage collector related slowdowns.
- Avoiding fragmented memory and garbage collection with object pools.
- Ideal Memory Allocations.
As game developers, we want our games to be performant and to run at a high frame rate so users have a smooth and enjoyable experience. Bad memory management practices can affect the user experience by causing sudden drops in performance and even crashes. In this chapter, we’ll explore when to allocate and deallocate our data to minimize triggering the garbage collector, which can negatively affect performance. We’ll also look at how we can avoid memory fragmentation, which can lead to unexpected out-of-memory crashes. We’ll also look at popular memory management solutions like object pools and how to implement them in DOD to maximize our performance.
3.1 Memory fragmentation
Memory fragmentation occurs when we have holes in our heap memory, as seen in Figure 3.1.
Figure 3.1 An example of fragmented heap memory, with holes of unused memory surrounded by allocated memory.
