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. Avoiding memory leaks

Avoiding memory leaks

Scheduled Pinned Locked Moved C#
csharpdelphidata-structuresperformancetutorial
5 Posts 5 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.
  • D Offline
    D Offline
    dwolver 0
    wrote on last edited by
    #1

    Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...

    C C N 3 Replies Last reply
    0
    • D dwolver 0

      Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      dwolver wrote:

      i'm not sure how true that is and how deep it goes.

      It is very true and it is very deep (but only for managed objects)

      dwolver wrote:

      i use this object in some button click for example, when the click is done will MyObject and all its other objects get released?

      Yes, eventually. The garbage collector does not run as soon as something is no longer referenced anywhere, it runs when it needs to. And then it will clean up the objects.

      dwolver wrote:

      Do I need some special routine that the system will automatically call when it goes to dispose of MyObject?

      Now, you've hit on the word I was going to mention next, "Dispose". If an object uses unmanaged resources then you must implement the IDisposable interface. This requires you to implement a method called Dispose() that is used to clean up the objects that the garbage collector cannot. So, if you use unmanaged objects or another class that implements the IDisposable interface (like a file stream) then you are responsible for cleaning up the object. You can call Dispose() manually, but a much neater solution is something like this:

      using (FileStream fs = new FileStream(someFileName))
      {
      // Do something with the FileStream
      }

      What happens here is that you tell the compiler that when you exit the using block to call Dispose on the object you created at the start. It doesn't matter how you exit the block. You can call return to exit the method as a whole, an exception can be thrown, whatever. The compiler will ensure that the object's Dispose method is called on your behalf.

      * Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay


      Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

      R 1 Reply Last reply
      0
      • D dwolver 0

        Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        THe fundamentals of .NET are that it manages your memory for you. If an object does not have a Dispose method to call ( and if it does, you should call it ), then you shouldn't have to worry about memory. That's what garbage collection means.

        Christian Graus Driven to the arms of OSX by Vista.

        1 Reply Last reply
        0
        • D dwolver 0

          Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...

          N Offline
          N Offline
          Najmal
          wrote on last edited by
          #4

          hi.......... Donn't worry abt memory leaks.. U know one of the important featuere of c#.NET is Its Effective Garbage Collection feature..... If u want to know more , just click below link.... About Garbage Collection

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            dwolver wrote:

            i'm not sure how true that is and how deep it goes.

            It is very true and it is very deep (but only for managed objects)

            dwolver wrote:

            i use this object in some button click for example, when the click is done will MyObject and all its other objects get released?

            Yes, eventually. The garbage collector does not run as soon as something is no longer referenced anywhere, it runs when it needs to. And then it will clean up the objects.

            dwolver wrote:

            Do I need some special routine that the system will automatically call when it goes to dispose of MyObject?

            Now, you've hit on the word I was going to mention next, "Dispose". If an object uses unmanaged resources then you must implement the IDisposable interface. This requires you to implement a method called Dispose() that is used to clean up the objects that the garbage collector cannot. So, if you use unmanaged objects or another class that implements the IDisposable interface (like a file stream) then you are responsible for cleaning up the object. You can call Dispose() manually, but a much neater solution is something like this:

            using (FileStream fs = new FileStream(someFileName))
            {
            // Do something with the FileStream
            }

            What happens here is that you tell the compiler that when you exit the using block to call Dispose on the object you created at the start. It doesn't matter how you exit the block. You can call return to exit the method as a whole, an exception can be thrown, whatever. The compiler will ensure that the object's Dispose method is called on your behalf.

            * Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay


            Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            Excellent answer. I'd just stress that the key sentence is: So, if you use unmanaged objects or another class that implements the IDisposable interface (like a file stream) then you are responsible for cleaning up the object. Too often seen replies that say in .Net you can completely disregard memory management issues because of automatic GC. Regards David R

            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