Question about pre-allocating memory for assets

How does one pre-allocate the correct amount of memory for game assets?

I have been working my way through handmade hero. I have watched the first 50 episodes and a hand full of the later episodes.

One concept that really interested me was the practice of pre-allocating all the needed memory for a game right at the start. From what I understand, Casey has basically grabbed a large chunk of memory and has used it for some of the game data and tile maps.

However, he currently(around ep 50) seems to allocate new memory every time he loads an asset file like a bitmap or such. I assume eventually this data will be will be converted to a different format and be loaded into the memory that he has pre-allocated instead of just allocating new memory. This means the pre-allocated memory would need to be large enough for the largest possible amount of assets that can be shown at once correct? How would one know how much memory to allocate? Would one try to calculate it by hand, or just play through the game with a very large memory allocation and record largest amount of memory needed during the playthrough then reduce the pre-allocation to that size. What if you missed an asset intensive event that could occur and thus have the wrong size?

Am I overthinking things? I have tried jumping around to a few episodes to find the solution, but am still confused on how one would do this. It seems like a very difficult problem to solve if a game has lot of detailed assets.

Any thoughts?

or just play through the game with a very large memory allocation and record largest amount of memory needed during the playthrough then reduce the pre-allocation to that size.

Yes, pretty much this.

What if you missed an asset intensive event that could occur and thus have the wrong size?

Then you have a bug. Fix it and release a patch :)
Testing for max memory this way is pretty much same testing as if you would test your game logic that you don't get stuck, or you cannot get out of level bounds and fall through world, or AI does not hang and go into some infinite loop, etc...


Edited by Mārtiņš Možeiko on

Thanks for the response!

I was worried that might be the answer haha. The problem doesn't seem too bad on a game with smaller games with or games with individual levels. However, it seems like this issue would be really hard to solve for in a game like animal crossing with a ton of potential models that might need to be loaded into memory.

Thank you for the help!


Replying to mmozeiko (#26016)

There are also ways to mitigate the issue, like having "required" assets and "optional" assets. For example the player model would be required, but a painting on a wall would be optional.

You could also use "level of detail" to use less memory (lower resolution model or texture) or display placeholders if you failed to load something.

That make sense. I guess there are some ways to elevate this issue. Having an placeholder asset for each type of asset seems like a good alternative to just stopping the game haha.

It is nice to be aware of these methods. It seems like you could do a similar thing for entity management as well. You could mark some entities as required and optional and not create optional entities if you have no more memory.


Replying to mrmixer (#26020)