Memory leak woes.
-
Hey everyone, I'm still chugging away on my little game. Right now you can connect to a server and play across the internet. The 'game' works, but I'm having some serious issues with memory leaks. I don't know when I should implement the IDisposable interface. Even when I implement it, it doesn't seem to make any difference at all. It seems the source of my problem are arrays and their values. For example: I had an array of bullets. Whenever one bullet was spent and no longer being used, I would just simply call: bulletArray[i] = new Bullet(); and replace the old version of the object with a new one. This caused a very unwelcome memory leak that took me hours to track down. I thought I could just overwrite the old object but it turns out the system holds on to it indefinitely. My next problem. I've recently started using ArrayLists. Whenever something was no longer needed, I would .Remove(ObjectNoLongerNeeded). Wouldn't removing the object also set it up for garbage collection? It doesn't seem to. What do I need to do to get rid of the old object and reclaim the memory? This is my first C# program and the first time using a OO language. It's wonderful to see my program working, but whenever anyone shoots while connected their program's memory requirement permanently increases. Whenever anyone explodes, memory requirement goes up. I know this has to do with my arrays, but damn, there are so many of them. How can I get around this? If I have a class called MyClass() that implements IDisposable, what do I need in the Dispose function to actually 'dispose' it? How do you dispose integers, floats, and other primatives? Thanks, EvilDingo
-
Hey everyone, I'm still chugging away on my little game. Right now you can connect to a server and play across the internet. The 'game' works, but I'm having some serious issues with memory leaks. I don't know when I should implement the IDisposable interface. Even when I implement it, it doesn't seem to make any difference at all. It seems the source of my problem are arrays and their values. For example: I had an array of bullets. Whenever one bullet was spent and no longer being used, I would just simply call: bulletArray[i] = new Bullet(); and replace the old version of the object with a new one. This caused a very unwelcome memory leak that took me hours to track down. I thought I could just overwrite the old object but it turns out the system holds on to it indefinitely. My next problem. I've recently started using ArrayLists. Whenever something was no longer needed, I would .Remove(ObjectNoLongerNeeded). Wouldn't removing the object also set it up for garbage collection? It doesn't seem to. What do I need to do to get rid of the old object and reclaim the memory? This is my first C# program and the first time using a OO language. It's wonderful to see my program working, but whenever anyone shoots while connected their program's memory requirement permanently increases. Whenever anyone explodes, memory requirement goes up. I know this has to do with my arrays, but damn, there are so many of them. How can I get around this? If I have a class called MyClass() that implements IDisposable, what do I need in the Dispose function to actually 'dispose' it? How do you dispose integers, floats, and other primatives? Thanks, EvilDingo
hmm, I know its a silly question but are you actually calling
GC.Collect
? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemGCClassCollectTopic.asp[^]
"When the only tool you have is a hammer, a sore thumb you will have."
-
Hey everyone, I'm still chugging away on my little game. Right now you can connect to a server and play across the internet. The 'game' works, but I'm having some serious issues with memory leaks. I don't know when I should implement the IDisposable interface. Even when I implement it, it doesn't seem to make any difference at all. It seems the source of my problem are arrays and their values. For example: I had an array of bullets. Whenever one bullet was spent and no longer being used, I would just simply call: bulletArray[i] = new Bullet(); and replace the old version of the object with a new one. This caused a very unwelcome memory leak that took me hours to track down. I thought I could just overwrite the old object but it turns out the system holds on to it indefinitely. My next problem. I've recently started using ArrayLists. Whenever something was no longer needed, I would .Remove(ObjectNoLongerNeeded). Wouldn't removing the object also set it up for garbage collection? It doesn't seem to. What do I need to do to get rid of the old object and reclaim the memory? This is my first C# program and the first time using a OO language. It's wonderful to see my program working, but whenever anyone shoots while connected their program's memory requirement permanently increases. Whenever anyone explodes, memory requirement goes up. I know this has to do with my arrays, but damn, there are so many of them. How can I get around this? If I have a class called MyClass() that implements IDisposable, what do I need in the Dispose function to actually 'dispose' it? How do you dispose integers, floats, and other primatives? Thanks, EvilDingo
Interesting problem. You only need to implement IDisposable if you have either unmanaged resources (i.e. window or other object handles from the OS) or scarce managed resources (i.e. database connections). All other managed classes, including arrays, should be collected by GC periodically. In fact, implementing IDisposable imposes a small performance penalty when it comes to garbage collection. Not that you should never do it, but only do it when necessary. I have heard of other situations like this. I don't know if it was a problem with the Framework or simply sloppy programming. You may look into using structures instead of classes, if your classes are very simple and contain mostly primitives. Structures can have constructors, properties and methods. They are like light-weight objects, but are created on the stack instead of the heap. HTH
-
Hey everyone, I'm still chugging away on my little game. Right now you can connect to a server and play across the internet. The 'game' works, but I'm having some serious issues with memory leaks. I don't know when I should implement the IDisposable interface. Even when I implement it, it doesn't seem to make any difference at all. It seems the source of my problem are arrays and their values. For example: I had an array of bullets. Whenever one bullet was spent and no longer being used, I would just simply call: bulletArray[i] = new Bullet(); and replace the old version of the object with a new one. This caused a very unwelcome memory leak that took me hours to track down. I thought I could just overwrite the old object but it turns out the system holds on to it indefinitely. My next problem. I've recently started using ArrayLists. Whenever something was no longer needed, I would .Remove(ObjectNoLongerNeeded). Wouldn't removing the object also set it up for garbage collection? It doesn't seem to. What do I need to do to get rid of the old object and reclaim the memory? This is my first C# program and the first time using a OO language. It's wonderful to see my program working, but whenever anyone shoots while connected their program's memory requirement permanently increases. Whenever anyone explodes, memory requirement goes up. I know this has to do with my arrays, but damn, there are so many of them. How can I get around this? If I have a class called MyClass() that implements IDisposable, what do I need in the Dispose function to actually 'dispose' it? How do you dispose integers, floats, and other primatives? Thanks, EvilDingo
Hi I know this does not answer your question, but I hope you know about the problems with ArrayLists. If you dont know these problems, these are what they are. Once you have set up an array to hold a certain number of items, it works fine. When you start increasing the size dynamically, speed drops bigtime, especially if you have large arrays or lots of them. The way it works, is if it can store 10 ints, and you add another 1, it will make a new array, and copy all the items into it, and hopefully delete the old one.