Game Development Community

Torque X huge memory leak - Fixed

by Giuseppe De Francesco · in Torque X 2D · 03/18/2010 (11:54 am) · 298 replies

Well,

I'm not sure this is the right place to post this, maybe the blog? At any rate I'm posting here now.

Torque X has a huge memory leak, but seriously! I was working to a RPG game for a start so I didn't really notice, but 2 weeks ago I started YASS, a multiplayer side scrolling space shooter: a lot of particles, really a lot, and a lot of projectiles as well.

To let you understand here follows a short video of the game:



This is the memory situation after 3 minutes of gameplay (single player!!!) the game slowed down a lot:

forum.xnaitalia.com/download/leak1.png

So, I searched the forum and I realized that this issue is an old one and GG never got a grip on it (go figure why...) so I decided to fix the problems myself because I really need to publish YASS ;)

Long story short: I revised the whole engine and made a lot of fixes, some of them really of great importance, so here it is the situation playing my game (as you can see after 9 minutes the game still is 20% less than the original 3 minutes!):

forum.xnaitalia.com/download/leak2.png

There are still many stuff to fix, but at any rate in the current state I'm able to publish the game ;) I will make the SVN diff files available upon request (free of charge: pino AT dftgames DOT com).

Cheers,
Pino

About the author

In the software eng. field since 1981, in charge of R&D during last 10 years. IEEE Senior Member (and volunteer).

#221
04/25/2010 (10:22 am)
@Tony: I just replied to that email... I didn't see it... really I'm getting too old :) As I wrote you can create a folder in the Community Project: a few test environment will be good in that area.

Thanks,
Pino
#222
04/25/2010 (1:18 pm)
I just replied to your email. Please let me know if you don't get it.
#224
04/25/2010 (2:19 pm)
Guys,

about the coloured terrain chunks, that's not a real error: it comes from the terrain "LOD Error" which seems wrongly deserialized: if you set that to an integer greather than 2 all works fine. I'm looking into this now waiting for the unit test project ;)
#225
04/25/2010 (3:08 pm)
Ok,

about the colours: I think that in the terrain demo that's made on purpose. The Terrain default LOD Error is 2, while in the FPS Demo is set to 0.5, so from what I see it's normal that we see those colours.

The previous version of the TorqueXMLDeserializer didn't have a type parser and in the Core distributed with TX3D 3.1.4 is missing as well thus I guess that it was converted to a different value. The fixed Core does translate that value properly so what you see is correct.

Just set the LOD Error to 2 and it will work properly.

Cheers,
Pino
#226
04/25/2010 (3:23 pm)
Heya so I found time to update the engine with the async loader. Its Amazing! I do have a few issues I need to work out.

I'm loading 2 scene files. One when the game first starts up. I then rename this scenegraph.

Then later once the player has selected their characters I load the level scene file. Once loaded I rename this sceneGraph to "DefaultSceneGraph" (I explane later that for some reason this isn't being set as the scenegraph name by default).

My biggest problem is with unloading. When I unload the level scene file with the following code:
Game.Instance.SceneLoader.Unload(@"data\levels\birthplanet.txscene");

I get the following errors.
"TorqueObject still added"
this error happens at the _sg.Reset(); line in the SceneContainer.cs

If you ignore the above error the following error displays
"TorqueObjectDatabase.Unregister- Unregistering an object which isn't properly registered."


The above errors are ignorable and the game runs fine if you ignore them (though I'm not sure that the object is removed from memory).


Also I noticed something that is a bit strange. So I noticed the my SceneGraphs dont seam to be created with the "DefaultSceneGraph" name as the following code:
if (_sceneGraph == null)
                _sceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph");
found in the T2DSceneObject OnRegister method is alweays called and the _sceneGraph is null after searching for the "DefaultSceneGraph" it seams my scene graphs are being created with their name set to "". Is this normal?


Also I have a question about dumping sceneobjects to a new scenegraph and unloading.

Right now in order to get my multiple scenegraphs to render I dump them into a shared scenegraph with the following code:
List<object> objects = sceneData.Objects;
            int count = objects.Count;

            
            for (int i = 0; i < count; i++)
            {
                T2DSceneObject obj = objects[i] as T2DSceneObject;

                if (obj != null)
                {
                    obj.SceneGraph = Game.Instance.SharedSceneGraph;
                }
            }

When I unload my "level" scene will it remove its objects even though I moved them to a new scenegraph?

Thanks for the help :).

#227
04/25/2010 (4:51 pm)
Hi Matt - lots of interesting stuff there. As you know, the async loading isn't fully tested yet, so there are likely some potential issues to work through (looks like you found some already :-)

What I've done is create a thread on the svn project site - you can find it here: www.xp-dev.com/forum/topic/2529. I've copied your post to there and will post some answers (and some questions).

If you find it doesn't let you post there then let me know and I will create a thread in the community project site instead (which is open access).
#228
04/25/2010 (5:03 pm)
Thanks my man. :)

I don't have permission to post in that section.

I'll do my best to help you with debugging the Async loader. It is a very exciting feature.

Thanks again.
#229
04/25/2010 (5:27 pm)
Thanks matt - appreciate the help. I've moved it over to the community forums instead: www.xp-dev.com/forum/topic/2554
#230
04/25/2010 (7:26 pm)
Hey all. I committed some new changes to the 2D and Core projects.
For the 2D project, I added the following fixes from this forum:
www.torquepowered.com/community/forums/viewthread/109995
www.torquepowered.com/community/forums/viewthread/107407

I also added my own feature to the 2D project where you can modify the positional offset of a mounted object without dismounting and remounting it.

For the Core project, I added these fixes from the forum:
www.torquepowered.com/community/forums/viewthread/97467/2#comment-684293
www.torquepowered.com/community/forums/viewthread/94107/1#comment-638285

I also added a some of my own bug fixes and features to the Core project, including some crash fixes, more helpful error messages, new functions, and the ability to scale and mirror GUIBitmaps.
#231
04/25/2010 (10:47 pm)
Pino I just added my 3D test, I made most of it with the 3D editor, did touch the xml a bit, it does some stupid stupid things, like making the collision sphere below the object. I also added a fully animated character with collision etc. You can see that the guy eventually just sinks below the sands.

I know that this has been the major source of frustration, probably #1 with the users. They follow the book, follow the directions given and still failure.
#232
04/26/2010 (1:19 am)
@Henry & Tony: Thanks for the two projects guys. Now... I tested both projects with the original libraries shipped with TX3D and then with the fixed shared Core along with the partly fixed TX3D module and... I can't see any slowness, actually with the fixed libraries the camera moves a little faster, that's all I can see on the Xbox.

This is getting difficult: any way to have a repro of the frame rate loss in a unit test project?
#233
04/26/2010 (3:46 am)
This line in T2DSceneObject.cs can cause some problems:
_sceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph"); // DBC - get the DefaultSceneGraph rather than just the first one we come across - this way, if we make sure scenegraphs are renamed after loading each scene then we know that DefaultSceneGraph is always the currently loading level and thus will load into the scenegraph that we want it to load into rather than whichever one happens to be first in the database
It is especially prevalent when using the asynchronous scene loader where the recommendation for the loader is to rename your scene graph after the scene is loaded. Unfortunately, this will break the dynamic registration of any new objects once the level is loaded.

All these hacks around the scene graphs all cause little issues. Does Torque X have any built-in scene graph managers, or will I have to build one? If Torque X doesn't have any built-in classes for managing scene graphs, I will probably start work on a TorqueSceneGraphDatabase tomorrow and work on modifying the engine to use it.
#234
04/26/2010 (6:08 am)
I'm not sure if we need a scenegraph database, but I was thinking of adding a CurrentSceneGraph variable. When a level is loaded CurrentSceneGraph would be set to the scenegraph for the level being loaded - all new objects would then be automatically added to that scenegraph until CurrentSceneGraph is changed (either directly or by loading a new scene). Renaming a scenegraph after a scene is loaded would no longer be an issue (unless you still need to access them by name, of course - which would not interfere with the CurrentSceneGraph thing anyway).

It would be similar to how the database has a 'CurrentFolder' for deciding what folder to add objects to when they are created. A CurrentScenegraph property seems to have been ommitted from the engine though. I can add this modification when I get back tonight.
#235
04/26/2010 (6:10 am)
@John - also there is a forum on the community wiki www.xp-dev.com/forum/1897/0
#236
04/26/2010 (6:36 am)
@Pino: Ok, this is not good. When I run the same project I uploaded on my xBox it is slow when you are looking at the zombie models that are in a row. If you just turn the camera in a circle I can see that is goes at a normal rate when not looking at the zombies, but as soon as they come into view it slows down. Not as much as my Zombie game slows down, but noticable. You don't see any slow down at all?
#237
04/26/2010 (7:43 am)
@Tony: I do see the camera slowing down a little when looking at the zombies but it's barely noticeable. I'm cleaning up the rendering code so those issues shoiuld go away, but the frame dropping to less than 15 like the Physics demo is not there at all.
#238
04/26/2010 (7:56 am)
@Pino: Well, let me take a look at my game code and see if I can figure out what I didn't put into the test application.
#239
04/26/2010 (3:12 pm)
Scenegraph hackery has been removed and replaced with much nicer CurrentSceneGraph property in the database.
#240
04/26/2010 (5:23 pm)
@Pino: You must have fixed the problem without realizing it. I have tried to reproduce the problem in my zombie game and it doesn't happen anymore.

This is hopeful. I don't know why the physics demo does it, however. I am trying to integrate my own fixes into your fixes and maybe I am still missing something, but at this point it looks fine.