Jump to content
  • Advertisement
Sign in to follow this  

How to detact memory leak and memory fragment efficiently

This topic is 571 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 ,all

    Our game has some problems with memory, there are two many third party library in our game . most of them  are closed source, I have some trouble with profilering it . I am newbie for this ,I  hope I can have some insight here.


           After running a long time ,  allocing memory will be failed.


          Reason 1: There may be some memory leak when game running.

         Reason 2:  After a long time running , there may be memory fragmentation in the memory space!


        For reason 1,  I tried to hook heapalloc and heapfree , then output some log for analyzing , but if I hooked these api ,the game run very slowly, which will lead to net error happenning , the test failed;  I also try umdh in windows debugging tools,  it has help me find some leak.

Is there any other way help me to find a leak in the third party library???

      For reason 2, I use vmmap to see the unused space.  Is there some other way to find the code which caused these memeory fragment?

Is there any suggestions for memory manangement with so many third party library in a game?

Share this post

Link to post
Share on other sites

As Sean said, allocate as little as possible.  Figure out you need 50,000 particles at once, but never more.  Then preallocate 51,000 or some safety buffer.  Then use asserts if you ever breach the 50,000 mark.  Recycle already allocated memory with memory pools, etc.

Share this post

Link to post
Share on other sites
The most common solutions I've seen in my work:

1) hook your allocation and free functions and make them record a log of timestamps/pointers/call-stacks. Write A GUI app that can analyse these logs and visualise address space over time.

2) In a level based game, try to make as many of your allocations occur during the loading screen as possible, and as few during gameplay as possible.
Also,enforce a rule that any allocation created during loading/gameplay for a level will be released when the level is over. Create a system that enforces this in code and asserts if an allocation from a level persists longer than that level. This is one of the main causes of fragmentation in games (often repeatedly loading a level amd quitting back to the main menu will eventually lead to a fragmentation based crash) so it makes sense to ban it.

You can also buy this: http://www.juryrigsoftware.com/Elephant/Goldfish

Share this post

Link to post
Share on other sites

I head that valgrind is quite popular, I have not used it myself but I friends that does and praise it.
If you want to try it out you can find it here:



Valgrind is a poor fit for games, unfortunately. Its biggest problem is that it effectively runs as a full emulator and will make a game run 100x slower; if just hooking allocations is too slow, that's certainly not possible. Valgrind also isn't compatible with Windows which is a deal breaker for most game developers.

There are toolkits that do similar things which manage to be both far faster and Windows-compatible, though perhaps without quite as much accuracy.

There are also free tools out there written by gamedevs like https://mtuner.net/


Highly recommended. One of our engineers just hooked this up to our (big AAA) game recently and we've been impressed. A custom tool like Hodgman recommended can still have a lot of benefits that you won't get with MTuner but a custom tool would probably be overkill for the OP.

Someone needs to write one of these as open source. It's an easy enough problem to tackle for someone with the time and experience. :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!