Jump to content
  • Advertisement
Sign in to follow this  
alectora

Memory management issue for external libraries

This topic is 561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys..

So I'm at the point where memory management becomes quite a tedious and daunting task. I'm currently making a game for mobile device, and I allocate one big chunk with a hope
all (most) memory allocation would strictly use that so I can track them while also limit the memory usage just to that chunk I take from a single malloc.

Now the issue isn't how I handle that big chunk, but the libraries that use its own internal allocator, especially in C.
For STL, globally replacing new and delete (and its array forms) works. Then Vulkan is almost the same as making custom allocator for STL container... and then here come other libraries
like FreeType, sqlite3, and I don't know what else will come when I need them. Some of them are even harder to handle!

My lazy brain starts to think to just let the memory usage grow instead of restricting them in one big chunk I allocated, it may seem to be not a bad idea it says...
but then the issue is I am unable to track them, and again it grows.
I'm not sure if my game can keep up in a phone with the memory suddenly being full when I need them during the game.
I also may not be able to track them of which does that.

What's your opinion to handle this situation? Do you soldier through replacing/creating custom allocators on these external libraries' allocator or just ignore them?

... or is it possible to replace global malloc/free? lol.

Share this post


Link to post
Share on other sites
Advertisement

Yeah most libraries that are aimed at gamedev / embedded use will give you the option to supply your own memory allocator, but in general, a lot of libraries just go ahead and use malloc/new :(

... or is it possible to replace global malloc/free? lol.

Recompile those external libraries with something like /Dmalloc MyMalloc / add a PCH that contains #define malloc MyMalloc / etc :)

 

On some platforms, there is an OS-level API that you can use to supply your own allocator which will be used internally by any calls to malloc/new. I haven't done iOS work, so I don't know if this is the case there.

Share this post


Link to post
Share on other sites

Yeah most libraries that are aimed at gamedev / embedded use will give you the option to supply your own memory allocator, but in general, a lot of libraries just go ahead and use malloc/new :(

... or is it possible to replace global malloc/free? lol.

Recompile those external libraries with something like /Dmalloc MyMalloc / add a PCH that contains #define malloc MyMalloc / etc :)
 
On some platforms, there is an OS-level API that you can use to supply your own allocator which will be used internally by any calls to malloc/new. I haven't done iOS work, so I don't know if this is the case there.

That's a nice idea, adding a PCH for malloc on each library! I'll try that first. :-)

About the OS level API, where can I find them? I tried to search for such info in Google for either Android and Windows, but no luck. I probably don't get the right keywords yet. Edited by away2903

Share this post


Link to post
Share on other sites

Another trick you might be able to use when you are using external library / API when you cannot control the allocations, is to change how you handle the resources they provide.

 

Sometimes you can reuse them (and keep track of them) yourself instead of deallocating and reallocating them through the library. For instance, if you know your game characters are going to use a maximum of 100K of some resource each, and you have a maximum of 16 in play, you can allocate 16x 100K at startup and handle assigning the resources yourself, and be sure of no fragmentation issues crashes etc (at a cost of maybe some wasted space in each resource).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!