Where does my RAM go??

Hi. I am developing a fighting game in Blender and found a serious problem. Everytime I am going from battle arena scene back to player select scene, system RAM consumption is growing. If I go back 20 times scene is not even loaded and operating system freezes. Scenes in my game goes at standart framerate, but loading time is growing everytime I go back from player select scene. I tried everything to solve this problem, but nothing changed.
Then I realized that something is wrong with Game Engine or my graphic card. So I tried to make a simple scene with 15 cubes and add one logic brick: restart scene when A-key is pressed. Keyboard Sensor (A-key)------- AND -------- Game Actuator (Restart this Game)
If I run this scene and click A-key manytimes and am looking in Windows Task Manager the RAM consumption for blender is growing everytime I press A-key in game. If I do nothing RAM is still high and wont go down. It is doing the same with set scene actuator in my game. I have tried this simple scene with older versions of Blender and problem is still the same. So maybe it is my graphic card (I got latest drivers).
Is there problem with Blender, Graphic Card or Am I too dumb :(??? Please help.
My PC: AMD 1,5 GHz, 500 MB RAM; WinXP; NVIDIA GeForce 6150 (128 MB)

Memory leak,that sounds really nasty. You might want to post the blend, so we can try it ourselves. If you let others try to reproduce it, you can make sure that it is not caused by your system setup/configuration/drivers. Good luck.

Zaghaghi and Ovidiu are willing to make a roadmap for their works based on what we think the BGE needs. (I started the thread)
http://blenderartists.org/forum/showthread.php?t=109528

I have major bugs being fixed as a suggested top priority.

That’s the well known (or should be well known) scene change memory leak. C-106 Delta suggested in this thread http://blenderartists.org/forum/showthread.php?t=107296 that he could maybe fix it…

OK. I made a little example for that problem. Download this blend file (http://www.pharcom.ic.cz/apps/RAM_Problem.blend). Run Task Manager, after that hit P-key in Blender to run the Game. In the game hit manytimes A-key to restart scene and observe if the RAM consumption is growing in Task Manager.
I would be really glad if anyone else will experience the same problem and I would be really really glad if anyone could solve this problem for me. It will be unmotivating for me to continue doing any games in Blender knowing that players playing my games must always end entire game only to choose another character.
Thank you all.

Edit: I just looked in that link PSK131, but save/load and restart of the scene has same effect as “set new scene”. I was wondering, if there isnt some python stuff, that could free memory used by the game. In PYTHON is module named Garbage Collector which is dealing with memory leaks - I have tried some scripting, but It has no effect.

This is a known memory handling problem. It has not been resolved yet, but I imagine it could be solved soon if enough people wanted it to be.

I ran your test .blend just to observe the problem in action (I’ve known about it for a while, I’ve just never actually watched it). I can confirm to you that the problem also exists on my computer.

BTW, I could not find a program called “Task Manager.” What version of Linux are you using? I don’t think I’ve ever seen that program in any of the more popular releases. Anyway, I just used System Monitor instead, since it displays the sort of information you were talking about.

blendenzo, task manager is a windows thing. press ctrl+alt+delete and it brings up a window with a whole buch of options to monitor status/running programs and apps/and manage settings.
he also stated he’s using windows:::…am looking in Windows Task Manager the RAM consumption for blender…::: then again i mighta over-looked something and be wrong.
this should be fixed ASAP i think, before it screws up anyones PC if it hasn’t yet.

LOL… Sorry, I guess I sounded a bit to serious. I was actually being a jerk (in a friendly sort of way). I know what Task Manager is, I just wish you Windows guys would stop acting like the whole world used your operating system, that’s all.

See what I mean? I can’t do what REiKo said, because I don’t have a program called Task Manager.

Hahaha… True true, back on topic.
This is by far the lamest memory leak I have ever seen, it truly sucks! Does anyone know what in the source code is causing it?

stop acting like the whole world used your operating system, that’s all.

Ok, then. Thatd be about 70% of the world then. Maybe more. If you’re using Vista Home Premium, you can configure features for the desktop to show RAM usage, CPU usage, Calender, stuff like that.

I downloaded source code for Blender 2.45 and start to learn programming in C. These “memory leaks” in scene and game actuator are very disturbing. I want to make my games looking good, but if BGE never frees data collected in memory, I cannot do any complex scenes that are switching. Is there any Blender or Python stuff that helps by reading the source code of Blender and are there any notes from programmers on programming BGE?
Thanks for help.

Don’t restart the scene. Build your game and menu in separate blend files. Use the game actuator to start a new game when you want to go the menu or back to the game. If you have multiple levels make them like this: menu.blend, level1.blend, level2.blend, etc.

Thanks for help PSK131. I have done it like you said, but it hasnt solved that problem unfortunately. I have made examples where does this memory leak occur:
a) If you set a new scene (example: www.pharcom.ic.cz/apps/RAM_Problem3.blend)
b) If you restart a scene (example: www.pharcom.ic.cz/apps/RAM_Problem.blend)
c) If you add an overlay/background scene (example: www.pharcom.ic.cz/apps/RAM_Problem2.blend)
d) If you start a new game (example: www.pharcom.ic.cz/apps/RAM_Problem4.rar)
e) If you add an object to the scene (example: www.pharcom.ic.cz/apps/RAM_Problem5.blend)
Everytime you do this, all data from scene (or object data) will remain in memory and wont free until you end your game completely. It is true problem only if you are dealing with bigger scenes or multivertex objects, but still this problem meets everybody. I have tried to get rid of it in many ways, but I think It is possible only by coding the BGE. Does anyone know what module or part of BGE could make this problems?

d) Damn, I wasn’t expecting that. I was so sure this could work that I didn’t even test it myself. Maybe you could close the Blender completely (game actuator, ‘Quit this game’) and then start a new game (with blenderplayer of course). I guess you’d need some kind of external script or application for this. I’m interested to find a solution for this since my game is too large to fit in memory at once.

I figured something out… You can use this python script to start a new game:


import os
os.system("blenderplayer game2.blend")

Then use game actuator to ‘Quit this game’. There are just two problems:

  1. It starts a new game, but doesn’t end the first one until you close the new one too. I don’t know why this happens.
  2. Because the new game is started when the old game is still running, you may run out of memory if your game is too big (as in my case it is).

I have tried your code, but nothing changed. I think that problem is in entire Blender application. I was looking for usefull python modules in Python Manuals: There is a gc module (Garbage Collector) which is dealing with memory allocation and data garbage. I tried to use it in end of each scene to free memory, but it looks like it isnt working (there were no errors in Blender shell window).
I am working in Blender for 5 years and I can do only short scripts in python in my games, so I will be probably not able to find the problem, but still I have ideas and motivation and want to help to solve this.
( I looked in Blender source files in Dev-C++ application and maybe found some clues, but I dont know C/C+ language well, so I must learn, learn, learn… .)
Is there any tutorial how to check Blender source files for bugs?

The problem was os.system() command which didn’t allow the current process to terminate. I changed it to os.execl() and now it works. Here’s an example: http://users.tkk.fi/~pkangass/blender/setscene_mem_leak_workaround.zip
Tested with Windows XP & Python 2.5.

EDIT: Extract those files to you Blender folder and type “blenderplayer 1.blend” in command prompt to start. When in game, press space to switch between blends.

Yes, It is working :slight_smile: !!! I have tried it on my game project and it worked. You must get blenderplayer.exe from Blender directory to folder where you have your game. And you must add (when you have not python intalled) scripts copy_reg.py, npath.py, os.py, stat.py, types.py, UserDict.py, avcodec-51.dll, avformat-51.dll, avutil-49.dll, pthreadVC2.dll, python25.dll, SDL.dll, zlib.dll.
With this the memory leak problem is gone. !!!GREAT WORK!!!
P.S.: But there are other problem: files are bigger and I have experienced problems with sound blocks (when autopack is disabled)

Edit: I am sorry. The sound block problem was my fault (i forgot the relative paths), it is running now.

Well, you’d have to include those dlls with your game in any case unless it is extracted into the Blender folder. Needed python modules take under 100 KB so including them shouldn’t be any problem. Anyway, thanks for finding out which python modules need to be included. Only problem I know is that this workaround may look a bit unprofessional, since it returns to desktop for a while when it loads a new blend. You also need some kind of save/load script.

Yes, it is a little bit unprofessional . But it is a solution. And there is also a problem that all your source *.blend files are in the directory, so everyone can copy your models and ideas :(. I tried to turn all blend files into executables and your script is running wih them too. Maybe some encryption will solve safety of your ideas.
I am sure, that you read that Zaghaghi is going to solve these problems (http://blenderartists.org/forum/showthread.php?t=109528). I wish him luck :). And until then I will use your script, PSK131 (if you dont mind :)).