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.

3.2 Avoiding garbage collector related slowdowns

3.2.1 GC Issue #1 — memory isn’t freed immediately

3.2.2 GC issue #2 — performance

3.2.3 GC issue #3 — memory fragmentation

3.2.4 GC best practices

3.3 Avoiding fragmented memory and garbage collection with object pools

3.3.1 Common causes of memory fragmentation

3.3.2 Using object pools to avoid memory fragmentation and garbage collection

3.3.3 What to do when object pools run out of room

3.3.4 Object pools in data-oriented design

3.3.5 Common bugs caused by object pools

3.4 Ideal memory allocations

3.5 Conclusion

3.6 Summary