How to detact memory leak and memory fragment efficiently

Started by
6 comments, last by laiyierjiangsu 6 years, 11 months ago

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.

Situation:

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

Analyzing:

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!

Question:

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?

Stay hungry, stay foolish!

Advertisement

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

If hooking allocation functions is causing the game to run that slowly, that's probably an indication that you're doing _waaaaay_ too much allocation during the game loop.

Which in turn probably causes fragmentation.

Also means you've got that much more room to mess up and leak (though with modern C++ programming, that should be rare). It also could mean that fragmentation is leading to allocation failure.

An idealized game preallocates as much memory as it needs before entering a level or whatnot and never allocates again. Larger open world games can get by with good object pools and paging techniques, though this takes a lot of effort and discipline.

If your third party libraries are the source of allocations, you have some mitigations. Most third-party libraries made for games allow hooking the library's allocation routines. You could use those hooks to pass the libraries straight to the fast allocator and then only override the allocator for your game code, and verify whether your own code is leaking. Alternatively, you can enable the leak detector individually for each library to track down the culprit. If it's just fragmentation, hooking the offending library to use its own small block pool allocator may be the solution.

Sean Middleditch – Game Systems Engineer – Join my team!

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:

http://valgrind.org/

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.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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.

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

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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:

http://valgrind.org/

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. :)

Sean Middleditch – Game Systems Engineer – Join my team!

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

Thanks ,I will try it latter

Stay hungry, stay foolish!

This topic is closed to new replies.

Advertisement