How do games like GTA IV not fragment the heap?

后端 未结 6 1240
深忆病人
深忆病人 2021-01-31 05:21

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

相关标签:
6条回答
  • 2021-01-31 05:53

    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 :)

    0 讨论(0)
  • 2021-01-31 06:00

    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,
    0 讨论(0)
  • 2021-01-31 06:00

    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.

    0 讨论(0)
  • 2021-01-31 06:00

    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.

    0 讨论(0)
  • 2021-01-31 06:06

    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.

    0 讨论(0)
  • 2021-01-31 06:09

    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.

    0 讨论(0)
提交回复
热议问题