Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Memory leak woes.

Memory leak woes.

Scheduled Pinned Locked Moved C#
questioncsharpgame-devsysadmindata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EvilDingo
    wrote on last edited by
    #1

    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

    P C T 3 Replies Last reply
    0
    • E 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

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      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."

      1 Reply Last reply
      0
      • E 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

        C Offline
        C Offline
        CBoland
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • E 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

          T Offline
          T Offline
          totig
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups