The document discusses various techniques for optimizing dynamic memory allocation in games and applications, including:
1) Custom allocators that run in user mode can avoid the performance cost of context switching to the kernel for each memory request.
2) Stack-based allocators allocate a large contiguous block of memory and use a pointer to allocate from the top, avoiding fragmentation.
3) Pool allocators preallocate a large block of exact size and divide it into equally sized slots for allocating objects, ensuring no fragmentation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
37 views
Module2 2
The document discusses various techniques for optimizing dynamic memory allocation in games and applications, including:
1) Custom allocators that run in user mode can avoid the performance cost of context switching to the kernel for each memory request.
2) Stack-based allocators allocate a large contiguous block of memory and use a pointer to allocate from the top, avoiding fragmentation.
3) Pool allocators preallocate a large block of exact size and divide it into equally sized slots for allocating objects, ensuring no fragmentation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15
Memory Management
Dr. S. Graceline Jasmine, SCOPE
Vellore Institute of Technology, Chennai
Optimizing Dynamic Memory Allocation • Memory affects performance in two ways: – Dynamic memory allocation • Memory allocation cost – Memory access patterns • Small data- continuous memory rather than spread across Optimizing Dynamic Memory Allocation (Cont.,) • Limitation – Context-switch • from user mode into kernel mode, process the request, and then context-switch back to the program. • How to overcome? – custom allocator • Satisfy requests from a preallocated memory block • Runs in user mode alone • Avoid the cost of context switch Stack-Based Allocators Allocate a large contiguous block A pointer to the top of the stack is maintained Double-Ended Stack Allocators • A single memory block can actually contain two stack allocators
Example: Midway’s Hydro Thunder arcade game
The bottom stack is used for loading and unloading levels (race tracks) The top stack is used for temporary memory blocks that are allocated and freed every frame. This allocation scheme worked extremely well and ensured that Hydro Thunder never suffered from memory fragmentation problems Pool Allocators • Applications which require lots of small blocks of memory during runtime. • Preallocating a large block of memory whose size is an exact multiple of the size of the elements that will be allocated • A pool allocator allocates a chunk of memory once, and divides that memory into slots/bins/pools which fit exactly M instances of size N Pool Allocators - Example As an example, consider we want to have a maximum of 256 bullets in flight at the same time, each bullet having a size of 32 bytes.
Thus, the pool allocator would allocate
256*32 = 8192 bytes once, dividing it into slots which are then used for allocating/freeing objects of size 32. Single-Frame and Double-Buffered Memory Allocators All game engines allocate at least some temporary data during the game loop. This data is either discarded at the end of each iteration of the loop or used on the next frame and then discarded. This allocation pattern is so common that many engines support single- and double-buffered allocators Single-Frame Allocators • Implemented by stack based allocator • At the beginning of each frame, the stack’s “top” pointer is cleared to the bottom of the memory block. • The allocator will be cleared at the start of every frame • Limitation Memory block allocated out of the single- frame buffer will only be valid during the current frame Double-Buffered Allocators • A double-buffered allocator allows a block of memory allocated on frame i to be used on frame (i + 1). • For example, memory allocated during frame n is cleared at the end of frame n + 1, and those allocated during frame n + 1 will be cleared at the end of frame n + 2. • This allows us to nicely pass information to the next frame without worrying about leaking memory long- term. • It will come in handy for things like velocity calculations, which are things that require us to know the position of the object in the last frame. Memory Fragmentation Major problem with dynamic heap allocations is that memory can become fragmented over time.
When the number of holes becomes large, and/or the
holes are all relatively small, we say the memory has become fragmented.
The problem with memory fragmentation is that
allocations may fail even when there are enough free bytes to satisfy the request.
Virtual memory can resolve this problem, but most of the
game engines do not make use of virtual memory. Memory Fragmentation (Cont.,) Avoiding Fragmentation with Stack and Pool Allocators A stack allocator is impervious to fragmentation because allocations are always contiguous, and blocks must be freed in an order opposite to that in which they were allocated Avoiding Fragmentation with Stack and Pool Allocators • A pool allocator is also free from fragmentation problems. Pools do become fragmented, but the fragmentation never causes premature out of- memory conditions as it does in a general-purpose heap. Pool allocation requests can never fail due to a lack of a large enough contiguous free block, because all of the blocks are exactly the same size. Avoiding Fragmentation with Stack and Pool Allocators