Collisions Causing Game-Breaking Garbage Collection Issues
by Bryant Drew Jones · in Torque X 2D · 01/12/2010 (6:07 pm) · 11 replies
Hi there :)
I have a very simple project where you can move a ball around a scene and bounce it off of various obstacles. I've noticed that the game allocates 1-3MB of memory each time the player collides with another object (I just looked at the game's memory usage in Windows Task Manager). That number alone seems ridiculous, but it usually garbage collects without a hitch immediately after.
Sometimes, however, the game "forgets" to collect its garbage. The memory allocated during a collision will stack up to about 15MB in size at which point the game decides it's time to collect its garbage. The problem is that, when this happens, the game drops to about 1-5 frames per second and stops processing collisions until the garbage is collected. If the player is colliding with an object the moment the game decides to collect its big hunk of garbage, the player gets stuck inside the object, disappears off the screen, and can't move. It completely breaks the game.
I'm not sure what's causing this to happen or what I can do to fix it. Has anyone run into this problem before? Any idea what might be the problem?
You can download my project here for reference: www.sprybry.com/uploads/torquetest.zip. It takes about 2-3 minutes of bouncing to reach the breaking point. Sorry for the dysfunctional controls :)
Thanks for your help!
I have a very simple project where you can move a ball around a scene and bounce it off of various obstacles. I've noticed that the game allocates 1-3MB of memory each time the player collides with another object (I just looked at the game's memory usage in Windows Task Manager). That number alone seems ridiculous, but it usually garbage collects without a hitch immediately after.
Sometimes, however, the game "forgets" to collect its garbage. The memory allocated during a collision will stack up to about 15MB in size at which point the game decides it's time to collect its garbage. The problem is that, when this happens, the game drops to about 1-5 frames per second and stops processing collisions until the garbage is collected. If the player is colliding with an object the moment the game decides to collect its big hunk of garbage, the player gets stuck inside the object, disappears off the screen, and can't move. It completely breaks the game.
I'm not sure what's causing this to happen or what I can do to fix it. Has anyone run into this problem before? Any idea what might be the problem?
You can download my project here for reference: www.sprybry.com/uploads/torquetest.zip. It takes about 2-3 minutes of bouncing to reach the breaking point. Sorry for the dysfunctional controls :)
Thanks for your help!
About the author
www.SpryBry.com :)
#2
Thanks for the quick reply. Interesting. The task manager probably isn't the best tool to use for this job, but that doesn't take away from the fact that (at least on my machine) the memory usage increases each time I collide with something.
If I open the task manager when the game first launches, it takes up about 60MB in memory. If I then just let the player's ball bounce off of the ground continuously without hitting any of the other objects, the memory usage will slowly increase until it takes up 84MB. At that point, the game will freeze for 2-3 seconds and the memory usage will go down to about 70MB.
Do you know of any tools that might help diagnose this problem? I'm not sure how to approach this....
01/12/2010 (6:43 pm)
Hi Duncan,Thanks for the quick reply. Interesting. The task manager probably isn't the best tool to use for this job, but that doesn't take away from the fact that (at least on my machine) the memory usage increases each time I collide with something.
If I open the task manager when the game first launches, it takes up about 60MB in memory. If I then just let the player's ball bounce off of the ground continuously without hitting any of the other objects, the memory usage will slowly increase until it takes up 84MB. At that point, the game will freeze for 2-3 seconds and the memory usage will go down to about 70MB.
Do you know of any tools that might help diagnose this problem? I'm not sure how to approach this....
#3
When I ran the game the memory usage remained constant. In fact near the end of the run total memory usage on my pc dropped a few mb. But of course other apps are running at the same time.
Ah, that was missing from the original post. I'll retest with that scenario....
....no crash.
Everything is fairly stable. There is a gain of about 100k per minute. But nothing that would crash the game or cause massive amounts of garbage collection.
(Torque X does seem to have some leaks, although not enough to be an issue in any games I've done with it so far).
I used the tool available here: memprofiler.com/ It has a free 14 day trial version that I was trying out. I'm quite liking it as it has both realtime and snapshot capabilities.
01/12/2010 (8:43 pm)
The task manager has a large granularity when it displays memory usage and also includes anything the c# runtime is allocating because it *might* need it. And so on and so forth. All in all a murky way to look at memory usage.When I ran the game the memory usage remained constant. In fact near the end of the run total memory usage on my pc dropped a few mb. But of course other apps are running at the same time.
Quote:
If I then just let the player's ball bounce off of the ground continuously without hitting any of the other objects, the memory usage will slowly increase until it takes up 84MB.
Ah, that was missing from the original post. I'll retest with that scenario....
....no crash.
Everything is fairly stable. There is a gain of about 100k per minute. But nothing that would crash the game or cause massive amounts of garbage collection.
(Torque X does seem to have some leaks, although not enough to be an issue in any games I've done with it so far).
I used the tool available here: memprofiler.com/ It has a free 14 day trial version that I was trying out. I'm quite liking it as it has both realtime and snapshot capabilities.
#4
01/12/2010 (8:46 pm)
Garbage collection on Windows usually goes pretty much unnoticed, unlike on xbox where it can be an issue. Try running the game as the only running app and see if it still crashes.
#5
And I should have been more clear about the problem. My apologies. The game doesn't *crash*; it continues to run. But when it does do the big garbage collecting on my machine it temporarily drops to 1-5 FPS and stops processing collisions. After it's done collecting garbage the game returns to normal.
I'm trying to understand why my machine would be causing these problems and not yours....
01/12/2010 (8:52 pm)
Thanks, I'll have to try that profiler out.And I should have been more clear about the problem. My apologies. The game doesn't *crash*; it continues to run. But when it does do the big garbage collecting on my machine it temporarily drops to 1-5 FPS and stops processing collisions. After it's done collecting garbage the game returns to normal.
I'm trying to understand why my machine would be causing these problems and not yours....
#6
I wish I knew how to answer this problem,
but at the very I was able to repro it on my machine, so it's prolly not your system.
01/12/2010 (8:54 pm)
Hey, I downloaded the files and tested your project - doesn't seem to crash, but it does drop a massive amount of frames when it is sitting for about 2 - 3 minutes..I wish I knew how to answer this problem,
but at the very I was able to repro it on my machine, so it's prolly not your system.
#7
I ran the profiler, but I can't say I know what I'm looking for. I don't think my problem is a memory leak necessarily as it's all being collected. There's just a ton of excess memory needing to be collected, so it slows my game down to a halt (and breaks collision detection momentarily...).
01/12/2010 (9:34 pm)
At least it's not just me :)I ran the profiler, but I can't say I know what I'm looking for. I don't think my problem is a memory leak necessarily as it's all being collected. There's just a ton of excess memory needing to be collected, so it slows my game down to a halt (and breaks collision detection momentarily...).
#8
The only way I've gotten it to do it is to position myself in between the bumper and the world limit and ram into the bumper at the right angle(which hits world, the bumper, the world, increasing velocity very fast) causing velocity to go through the roof. The collision system with Torque sucks with super high velocities(ones that will cross more than a few screens in an update), so yeah. Anywho, it does it in such quick succession that your 90% drop per update doesnt do enough to curb this behavior.
Running the profiler you aren't getting a big leak or anything, in fact everything looks great for a torque game.
The frame dropping is common when you have an object sitting on another colliding. Processing a collision every update will cause some time loss. You can fix a bit of it by removing the ResolveCollision Delegate from everything but the player object as you aren't having them bouncing, just the player object.
01/13/2010 (1:26 am)
I haven't replicated the getting stuck in an object bug, but I did replicate the zooming off screen one...and i have a good idea what is causing it, and it's definitely not the garbage collector.The only way I've gotten it to do it is to position myself in between the bumper and the world limit and ram into the bumper at the right angle(which hits world, the bumper, the world, increasing velocity very fast) causing velocity to go through the roof. The collision system with Torque sucks with super high velocities(ones that will cross more than a few screens in an update), so yeah. Anywho, it does it in such quick succession that your 90% drop per update doesnt do enough to curb this behavior.
Running the profiler you aren't getting a big leak or anything, in fact everything looks great for a torque game.
The frame dropping is common when you have an object sitting on another colliding. Processing a collision every update will cause some time loss. You can fix a bit of it by removing the ResolveCollision Delegate from everything but the player object as you aren't having them bouncing, just the player object.
#9
01/13/2010 (12:58 pm)
Based on what Christopher just said you might want to enforce a maximum velocity on your object to stop the collisions getting out of hand.
#10
I'll go through and start removing the collision duplication you mentioned. I messed around with it a bit but it didn't seem to fix the framerate issue :\ I'll also try running it on multiple machines, including an Xbox. I might have to start ripping out all of my code (not that I have much) until the problem goes away (if it does). Oooh I'm so concerned....
01/14/2010 (5:41 pm)
Thank you both for your help. I'm glad you found that other bug, Christopher :) I was about to start another thread asking about that :) But I've seen that problem in other engines, so I'm not surprised that's what the issue is. I'll cap the velocity and reduce the bounciness of the bumper.I'll go through and start removing the collision duplication you mentioned. I messed around with it a bit but it didn't seem to fix the framerate issue :\ I'll also try running it on multiple machines, including an Xbox. I might have to start ripping out all of my code (not that I have much) until the problem goes away (if it does). Oooh I'm so concerned....
#11
01/14/2010 (7:52 pm)
Well, Torque is suited for platformers. Its also how the standard collision delegates handle. Ive only used them for world boundries and platforms(in a platformer id have the actor with just a tip of their collision hit the floor as many sides can create weirdness). I like to do my own custom delegates for most other things.
Torque Owner Duncan Colvin