Skip to main content
Share Your Experience: Take the 2024 Developer Survey
63 votes

How do C++ games handle memory allocation failure?

Same as all average programs: They don't.* For most applications there is no customer expectation that they continue working once memory runs out. All games fall under these "most applications". ...
Peter's user avatar
  • 9,925
37 votes
Accepted

Mechanics to keep mobs and environment alive without using tons of memory?

First of all, are you sure you really need that? Have you calculated the memory footprint? A small back-of-the-envelope calculation: A single mob and its state should fit into 100 byte of data. Let'...
Philipp's user avatar
  • 120k
30 votes
Accepted

How does GPU handle texture and VRAM?

GPU compressed texture formats like DXT / BC / ETC are specifically designed to be read directly from their compressed form. They don't need to be unpacked into a raw RGBA buffer. The way this works ...
DMGregory's user avatar
  • 135k
22 votes

How do C++ games handle memory allocation failure?

Typically this kind of scenario never happens. Firstly, virtual memory on modern operating systems means that it's highly unlikely to happen in normal operation anyway; unless you have a runaway ...
Maximus Minimus's user avatar
21 votes
Accepted

How do I know if memory isn't being handled right?

Virtual memory is allocated from the OS in large chunks, when even a byte is left still in use in that large chunk it cannot be released back to the OS. Allocation libraries in user-space also keep a ...
Stephane Hockenhull's user avatar
14 votes

Mechanics to keep mobs and environment alive without using tons of memory?

One way you could solve this problem is not actually storing state on disk, but just setting up your generation code to use a seed for the random number generator, so it generates the same thing for a ...
user14376's user avatar
  • 249
8 votes

How does GPU handle texture and VRAM?

Answers to your questions really depends on the platform. Generally BC formats are kept in compressed form in memory and decompression is done in flight by GPU texture units, but there are exceptions. ...
JarkkoL's user avatar
  • 870
6 votes
Accepted

How to prevent memory hacks?

I've seen some folks suggest obfuscating your variables something like this: ...
DMGregory's user avatar
  • 135k
4 votes

Is replacing a struct field in a class generating garbage using C#?

Simple Answer This isn't properly precise, but it's "good enough" for most day-to-day work: Value types like structs do not themselves create garbage collection overhead. If you already have some ...
DMGregory's user avatar
  • 135k
4 votes
Accepted

Efficient Dynamic Memory Management

deleteBuffer will delete the buffer in WebGL (and OpenGL) but I'm just curious, is there any reason not to just reuse the same buffer just put new data in it? (eg. have a pool of buffers?)
gman's user avatar
  • 840
4 votes

Unreasonably large memory use with high-res screenshot script

Textures, RenderTextures, and Materials use memory outside of C#'s managed heap (including ...
DMGregory's user avatar
  • 135k
4 votes

Mechanics to keep mobs and environment alive without using tons of memory?

Save it to the hard drive instead. Saving large quantities of data in a non-volatile fashion is literally why hard drives were invented. If a player isn't interacting with it anymore, you won't need ...
nick012000's user avatar
4 votes
Accepted

How do open world game engines allocate memory?

Creating a seamless open world without loading bars is not an easy task. There are lots of small and large problems which need to be solved. This is not a beginners project! The basic approach is ...
Philipp's user avatar
  • 120k
4 votes
Accepted

What is happening during queue_free() process?

Reproducing the error I managed to reproduce this reliably. It happens when calling queue_free on a Node that has already been ...
Theraot's user avatar
  • 27k
3 votes

Static variable pre-declaration & memory heap expansion in Unity

Local variables are not added to the heap. They are pushed onto the stack and immediately popped off the stack when they leave scope, thus already avoiding the heap (and the sort of memory allocation ...
Ed Marty's user avatar
  • 5,229
3 votes
Accepted

How can I make a custom memory allocation for classes with virtual methods?

Take a look at std::allocator_traits that will allow you to use your allocator and construct objects in allocated space. If you don't want to use that you can use placement new: ...
ratchet freak's user avatar
3 votes

Any advantage of having chunks with sizes by the power of two?

The main advantage of power-of-two sizes is that division by a power of 2 (on an integer) is just a bit shift, and modding is just a mask. Both of these operations are blazingly fast to do on gobs of ...
DMGregory's user avatar
  • 135k
3 votes

Is replacing a struct field in a class generating garbage using C#?

No, that line will not have any impact on the garbage collector. You are right in that the struct lives within the block of heap memory allocated for the class; the garbage collector only cares about ...
MikeP's user avatar
  • 325
3 votes
Accepted

About HLSL array packing policy

The rules are a bit convoluted but the essence is that if a vector element would wrap into the next register, it's put at the beginning of the next register. Your CPU-side and GPU-side definitions do ...
Lars Viklund's user avatar
  • 4,104
3 votes

RAM usage unusually high, but CPU usage relatively low

The Android memory profilers that comes with the Android Developer Tools will allow you to figure out what is consuming your memory. It comes in three parts; Memory Monitor (this is used to find ...
bornander's user avatar
  • 4,491
3 votes
Accepted

Should you avoid memory allocation and deallocation while the game is running?

I interpret this as meaning that you should do all the necessary heap allocation while the game is booting up, and arrange it in object pools that you then manage when the game is running. Is this ...
Philipp's user avatar
  • 120k
3 votes
Accepted

Rotating a Unity Texture2D 90/180 degrees without using GetPixels32 or SetPixels32? (To avoid heap fragmentation)

Two tricks we can use to get the RotateImage method down to zero allocations (in steady state): We'll use ...
DMGregory's user avatar
  • 135k
2 votes
Accepted

Is keeping all my game art in a static class wasting memory?

When LoadContent works as advertised, then it seems to load all resources from disk into memory. The result will be a longer load-time at the beginning of the game,...
Philipp's user avatar
  • 120k
2 votes

How do C++ games handle memory allocation failure?

Well, mainly, the same way we did it before exceptions existed - the old "check the return value approach". If an allocator doesn't use exceptions, it will usually return ...
Luaan's user avatar
  • 890
2 votes
Accepted

Is replacing a struct field in a class generating garbage using C#?

No, inspecting disassembly, correct me if I am wrong, appears to construct it in-place(compiled by VS2015 compiler) - lea loading the address of the struct and <...
wondra's user avatar
  • 4,930
2 votes

Voxel Game - High Memory and CPU Usage

First of all, we can say with absolute confidence that the culprit is not your glfwSwapBuffers call. On it's own SwapBuffers doesn't do much; what it does however do is take all of the GL commands ...
Maximus Minimus's user avatar
2 votes

Vulkan - preallocating chunks of memory and resource memory requirements

Or is it safe to assume that if it's a device local heap it will always be viable for device local resources? If it were safe to assume that, Vulkan wouldn't have APIs that specifically allow you to ...
Nicol Bolas's user avatar
  • 26.1k
2 votes
Accepted

How do open world games handle efficiently enemy respawn?

When you need to persist the state of a chunk but you are running out of memory, consider persisting it to the hard drive. If your game has enough complexity and data to warrant it, you might want to ...
Philipp's user avatar
  • 120k

Only top scored, non community-wiki answers of a minimum length are eligible