Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do games like GTA IV not fragment the heap?

I'm interested in the type of memory management a game like GTA IV might use given that it needs to create and delete lots of objects very quickly. How do the avoid fragmenting the heap and other things. If someone could point me in the right direction I'd really appreciate it.

like image 208
jmasterx Avatar asked Jan 14 '11 03:01

jmasterx


3 Answers

They use things like memory pooling, specialized allocators, and specialized container classes.

  1. The Hoard Memory allocator for multi-threaded programs.
  2. EA's version of the STL. Contains allocators, containers,
like image 176
wheaties Avatar answered Nov 07 '22 02:11

wheaties


While this paper does not specifically refer to games, it provides a good roundup of the kind of custom memory allocators that people often employ in C and C++ applications in an attempt to improve performance (and is a bit of a cautionary tale).

Reconsidering custom memory allocation, Berger, Zorn & McKinley, OOPSLA 2002

Programmers hoping to achieve performance improvements often use custom memory allocators. This in-depth study examines eight applications that use custom allocators. Surprisingly, for six of these applications, a state-of-the-art general-purpose allocator (the Lea allocator) performs as well as or better than the custom allocators. The two exceptions use regions, which deliver higher performance (improvements of up to 44%). Regions also reduce programmer burden and eliminate a source of memory leaks. However, we show that the inability of programmers to free individual objects within regions can lead to a substantial increase in memory consumption. Worse, this limitation precludes the use of regions for common programming idioms, reducing their usefulness. We present a generalization of general-purpose and region-based allocators that we call reaps. Reaps are a combination of regions and heaps, providing a full range of region semantics with the addition of individual object deletion. We show that our implementation of reaps provides high performance, outperforming other allocators with region-like semantics. We then use a case study to demonstrate the space advantages and software engineering benefits of reaps in practice. Our results indicate that programmers needing fast regions should use reaps, and that most programmers considering custom allocators should instead use the Lea allocator.

like image 32
EmeryBerger Avatar answered Nov 07 '22 03:11

EmeryBerger


There are two very good malloc implementations for multi-threaded implementations:

  • tcmalloc: by Google
  • jemalloc: used by Apache (for example)

Here is Facebook's article about improving jemalloc. It's about 5 times faster that Hoard's memory allocator in the current top answer :)

like image 7
Matthieu M. Avatar answered Nov 07 '22 02:11

Matthieu M.