Ways to prevent a possible memory leak?

Hey there,

Lately I’ve been noticing that my system almost runs out of memory after I’ve had Blender open for a few hours (say about 4+). I’m only working on code and testing it out in-game, so I’m led to believe that garbage is not being properly collected when I quit the game/end scenes, as this only happens after I’ve done so a couple hundred times.

So, question: can I ensure that memory is freed after I close the game? My only other alternative is close Blender anytime the slow down grows more significant.

Not everything is getting freed indeed. Best thing to do is close blender completely once an hour or so(at least this is what i do). it can even fill up my 16GB ram. I believe it has to do with faster startup/load times when you hit P, (like first time is slow, all time after that it loads faster).

But you’re not the only one with this issue.

i only ever use standalone mode, and if i do launch in the viewport, my game early bails so no libloading or objects get added.

that said, i have never had a memory issue in blender, other than running massive decimate or uv unwraps. i have afterburner put a ram meter in my tray so i know how much of my 16gb is being used at a glance.

I find that one good practice is to test the game with the Standalone Player as often as possible, since it spawns a process just for it, that dies once the game finishes, it reduces the effects leaks would have on the main Blender editor process.

Depending on how you develop your scripts, the leak could also come from you, but I don’t have a way of clearly identifying that case.

1 Like

Not going to deny this possibility, I write some nasty things.
Any reads/lectures you might recommend? I’m pretty lacking on the theoretical side of this.

I have done some hobby programming in C++. I want to give the advise I used for my own programs, to make certain all ram was free.

When you declare a pointer, which is used for holding the address for the allocation unit, then mark it as null. When your program exits, it calls a function at exit which runs through all the pointers you use. If it’s null, it leaes it alone. If it’s not null, it calls free.

During run time, if you release any ram, then mark the pointer as null.

For saftey, you can always check the pointers to see if it’s null before doing any ram access. If it’s null and the program is supposed to use it, then you can make a clean exit. Sometimes the allocation will fail.

Just google up "Python avoid memory leaks".

But the general idea is that anything you store will be kept alive as long as something references it.

Found this StackOverflow answer that explains the worst case scenarios:
https://stackoverflow.com/a/2017431/7983255

But in the case of Blender, there might be some quirks, as I have no idea what really persists between different embedded engine runs. The interpreter sure keeps on living (easy to leak threads btw).

I have noted that if I libfree my meshes I generate before shutdown this helps quite a bit