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 oth开发者_如何学Pythoner things. If someone could point me in the right direction I'd really appreciate it.
They use things like memory pooling, specialized allocators, and specialized container classes.
- The Hoard Memory allocator for multi-threaded programs.
- EA's version of the STL. Contains allocators, containers,
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.
There are two very good malloc
implementations for multi-threaded implementations:
tcmalloc
: by Googlejemalloc
: 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 :)
Creating and destroying lots of objects really quickly doesn't necessarily mean heap fragmentation. Often, if the objects are of the same size, then the space freed by one can be allocated for the next.
Often, even just as an optimization, a program might not free and delete memory for objects, but keep old ones in a pool and simply grab one and overwrite the contents to create a 'new' object.
I think because their objects are mostly of the same size they can use some internal memory manager which doesn't actually free memory, but marks it as available and next time chunk of ~size is allocated it returns it.
Lots of games use some of the memory allocators mentioned here. dlmalloc is one that we have used with great success (we have tied it into Lua). You can get information on dlmalloc here.
One other thing that I would mention though, is that not all games have to use dynamic memory allocation. We have employed mempools and use of static memory regions for most of our gameplay data. Only the systems that require dynamic allocation do so (Lua and some third party libraries). Everything else that we do comes out of static buffers. We use no STL in gamecode either, which helps limit the "damage" of memory fragmentation.
精彩评论