Jump to content

  • Log In with Google      Sign In   
  • Create Account

C#/XNA Simulations always get laggy over time


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Dagz   Members   -  Reputation: 110

Like
0Likes
Like

Posted 24 October 2012 - 09:54 PM

Hey so in the past I've been very interested in making simulations of evolving interesting behaviors between artificial intelligent agents. Obviously this sort of simulation is a whole lot of creating populations, killing them, creating more, killing them over and over. I've always had this problem, though, where as the program would run it would slowly get more and more laggy, until maybe thirty minutes into the simulation the lag would be so bad I'd have to stop it altogether. I've heard of this thing called "garbage collection" and suspect that is the issue, but I've got no idea what it entails. Does anyone have any advice to offer?

Sponsor:

#2 SelethD   Members   -  Reputation: 403

Like
0Likes
Like

Posted 25 October 2012 - 05:51 AM

I would highly doubt that its an issue with the 'garbage collection'. Its really hard to say without much details, however, I would suspect that its an issue with your code not releasing resources when it is finished with them.

If you are keeping your population within some sort of list, are you removing them from the list once they are 'killed', to keep your lists from getting extreamly huge?

I would check for problems like that first.

#3 MrDaaark   Members   -  Reputation: 3555

Like
0Likes
Like

Posted 25 October 2012 - 06:17 AM

How are you programming in C# and you don't know what garbage collection is? It's one of the most basic topics in C# and affects everything you do with it.

Garbage collection means that you don't have to handle de-allocating your objects, because the collector stops the program every now and then, and cleans up all the old resources that are no longer being used. This is not free, and there is a time cost to do this as the GC checks over the entirety of your allocated memory for dead resources to free. In more complex programs with lots of memory overhead, or frequent resource allocations this can introduce stuttering, and possibly lag in extreme cases.

A good way to avoid this is to make a fixed pool and allocate it up front. Then you can mark entries in this array/list/whatever as dead and re-use a dead entry when you need a new one. http://blog.gallusgames.com/programming/lean-mean-object-pool-in-c

Also, are you sure that you are properly killing off populations, and that the program isn't just doing more and more work as the game progresses?

#4 SeraphLance   Members   -  Reputation: 1454

Like
1Likes
Like

Posted 25 October 2012 - 08:29 AM

Garbage Collection exists to avoid one of the vectors towards the sort of symptoms you're experiencing -- that is, memory leaks which cause you to use more and more memory the longer your program runs. However, because you have a garbage collector in C#, it is unlikely (though not totally impossible) that you have any sort of explicit memory leak issue.

More likely you're just allocating too much memory. When the actors in your system die, you need to remove references to them (or reuse them in an object pool as Daaark said) or the garbage collector can't do its work. If you have half a million dead entities and 30 live ones, and all of your dead ones are just flagged with some isDead variable so the engine doesn't process their behavior, your engine still has to scan every single entity. Without any code, it's not really possible to know exactly where your problem is, but that's pretty much the gist of it.

#5 ATC   Members   -  Reputation: 551

Like
0Likes
Like

Posted 25 October 2012 - 11:35 AM

Hey so in the past I've been very interested in making simulations of evolving interesting behaviors between artificial intelligent agents. Obviously this sort of simulation is a whole lot of creating populations, killing them, creating more, killing them over and over. I've always had this problem, though, where as the program would run it would slowly get more and more laggy, until maybe thirty minutes into the simulation the lag would be so bad I'd have to stop it altogether. I've heard of this thing called "garbage collection" and suspect that is the issue, but I've got no idea what it entails. Does anyone have any advice to offer?


Sounds like your code is the problem; not the language or runtime. Since you're talking about garbage collection as if it were some mysterious thing you've only heard of before I assume you don't have a lot of C# experience or intimate knowledge of the language and CLR. But the type of project you're working on seems to suggest you have some significant programming experience in general (come from a C++ background?). So I suggest that you get a good book on modern C# and CLR and learn all the ins and outs of the language and how the runtime works; then leverage that knowledge to fix your code.

I've run flight simulations in C# that performed every bit as fast as comparable simulations written in C or C++, and could sometimes be about 36% faster. So there's little doubt in my mind that C# itself is NOT causing the problem. In general, C# applications have smaller memory footprints than native applications and are much more memory-efficient; and automatically managed memory can virtually eliminate memory leaks (save when dealing with native resources and unmanaged memory). This, alone, can make C# applications outrun comparable native applications, and C# has other significant advantages as well. So don't blame the language or the CLR; blame the application! Posted Image

Edited by ATC, 25 October 2012 - 11:36 AM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine


Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

#6 Dagz   Members   -  Reputation: 110

Like
0Likes
Like

Posted 25 October 2012 - 09:50 PM

Thanks for the quick replies. I think you guys are right, it must just be that I've written very poor code in the past. These are past projects that I'm talking about so while I'm not certain, I am pretty sure that I wasn't making any of the mistakes you guys spoke of. Nevertheless I did write up a simple 2D physics sim and left it running all day and it never slowed down due to lag.

ATC I'm not very experienced, but I've always been trying to take on projects that are outside my skill level. I'm very interested in AI and evolutionary algorithms though. As soon as school works slows I want to implement some new ideas and I'll have to scour through my code and make sure there are no memory leaks.

#7 PhillipHamlyn   Members   -  Reputation: 454

Like
1Likes
Like

Posted 26 October 2012 - 01:30 AM

Dagz

This is symptomatic of using Event Handlers but not unhooking them. Its a common mistake to make if you are new to .net. Look for any event handlers you hook up and make sure you are unhooking them deliberately at some stage. What tends to happen is they get hooked at some stage but never unhooked. This means that the class that has the event declaration has references to all those classes that hook that event, and if that event raising class is a static or long lived class, the classes that are hooking its events never get released to be GC'd - and even worse continue to have events raised on them long after their utility has gone.

This might not be precisely your problem but its a common anti-pattern that leads to memory loss and laggy behaviour in .net systems of all kinds.

#8 Dagz   Members   -  Reputation: 110

Like
0Likes
Like

Posted 27 October 2012 - 12:51 PM

Dagz

This is symptomatic of using Event Handlers but not unhooking them. Its a common mistake to make if you are new to .net. Look for any event handlers you hook up and make sure you are unhooking them deliberately at some stage. What tends to happen is they get hooked at some stage but never unhooked. This means that the class that has the event declaration has references to all those classes that hook that event, and if that event raising class is a static or long lived class, the classes that are hooking its events never get released to be GC'd - and even worse continue to have events raised on them long after their utility has gone.

This might not be precisely your problem but its a common anti-pattern that leads to memory loss and laggy behaviour in .net systems of all kinds.


I'm not sure what event handlers are or what it means to unhook them, but thanks I'll look into it. Google is my friend :P




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS