none
IDisposable.Dsipose() RRS feed

  • Question

  • hi,

    i  have this prob for a long time, if i created my own class (eg: student) and create and object(student1) in another class(School) how can i dispose the student1 object by using IDisposable.Dispose(). I did the code as same as mentioned in msdn but it wont work. are we need to bother on this or CLR manage this by itself and if CLR manage this what happen if the object(student1) take huge space in memory? please help me on this.

    Saturday, August 21, 2010 2:42 PM

Answers

  • The Garbage Collector will free objects once there are no longer any valid reference to them.  The only time you really need to consider implementing IDisposable is when your object uses a lot of resources (bitmaps, files, unmanaged resources, etc).  In those cases, you should implement IDisposable and in explicitly dispose of the resources inside your Dispose method.  Note that you need to call Dispose() explicitly unless you decide to implement a finalizer.

    That's the short answer, if you provide more details or a code sample maybe we can help more. 


    HTH,
    ShaneB

    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Saturday, August 21, 2010 2:51 PM
  • Unless you are making direct calls into the operating system to obtain memory or other resources (files, etc.) or your class contains fields that themselves have Dispose methods, usually there is nothing to worry about.  This is because the kinds of memory taken up by your object are those that the garbage collector automatically tracks.  The whole IDisposable thing is really just a workaround for releasing resources provided by the OS that the garbage collector does not automatically track.

    At an opportune time, the garbage collector will run and notice that the memory used by instances of your class is no longer reachable by anything in your program.  At this point, it will reclaim that memory for future use by your program (or perhaps give it back to the OS if a large enough contiguous chunk of it is no longer need by your program).  You can call GC.Collect() in your code to force the GC to run, but the system is designed to automatically run the collector at good times, so you usually should not run this on your own.

     

     

    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Saturday, August 21, 2010 7:23 PM
  • Hi,

    Thanks for your post.

    If there's no native resouces involved in the scenario, we can rely on the CLR to do the GC job.  If native resources are involved and need us to release them, we could refer to the dispose pattern. Please take a look at the following links, they could be helpful.

    1. Implementing IDisposable and the Dispose Pattern Properly
    2. Some collective info about IDisposable


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Monday, August 23, 2010 5:25 AM refine
    • Proposed as answer by Konrad Neitzel Monday, August 23, 2010 6:13 AM
    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Monday, August 23, 2010 5:25 AM

All replies

  • The Garbage Collector will free objects once there are no longer any valid reference to them.  The only time you really need to consider implementing IDisposable is when your object uses a lot of resources (bitmaps, files, unmanaged resources, etc).  In those cases, you should implement IDisposable and in explicitly dispose of the resources inside your Dispose method.  Note that you need to call Dispose() explicitly unless you decide to implement a finalizer.

    That's the short answer, if you provide more details or a code sample maybe we can help more. 


    HTH,
    ShaneB

    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Saturday, August 21, 2010 2:51 PM
  • Unless you are making direct calls into the operating system to obtain memory or other resources (files, etc.) or your class contains fields that themselves have Dispose methods, usually there is nothing to worry about.  This is because the kinds of memory taken up by your object are those that the garbage collector automatically tracks.  The whole IDisposable thing is really just a workaround for releasing resources provided by the OS that the garbage collector does not automatically track.

    At an opportune time, the garbage collector will run and notice that the memory used by instances of your class is no longer reachable by anything in your program.  At this point, it will reclaim that memory for future use by your program (or perhaps give it back to the OS if a large enough contiguous chunk of it is no longer need by your program).  You can call GC.Collect() in your code to force the GC to run, but the system is designed to automatically run the collector at good times, so you usually should not run this on your own.

     

     

    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Saturday, August 21, 2010 7:23 PM
  • can you send me a sample code and actually i want to how to call dispose method because even i call tha method it will not release the memory. following is the code. can u tell me how to pass the parameater to the constructor of the class Validation.

    internal

     

    class Validation

     

    {

    private IntPtr handle;

    private Component component = new Component();

     

    private bool disposed = false;

     

    public Validation(IntPtr handle)

    {

     

    this.handle = handle;

    }

            #region IDisposable Members

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            private void Dispose(bool disposing)
                {
                    if (!this.disposed)
                    {
                        if (disposing)
                        {
                            component.Dispose();
                        }
                        CloseHandle(handle);
                        handle = IntPtr.Zero;

                        disposed = true;
                    }
                }
                [System.Runtime.InteropServices.DllImport("Kernel32")]
                private extern static Boolean CloseHandle(IntPtr handle);

                ~Validation()
                {
                    Dispose(false);
                }
            #endregion
        }

     

    Sunday, August 22, 2010 11:26 AM
  • Hi,

    Thanks for your post.

    If there's no native resouces involved in the scenario, we can rely on the CLR to do the GC job.  If native resources are involved and need us to release them, we could refer to the dispose pattern. Please take a look at the following links, they could be helpful.

    1. Implementing IDisposable and the Dispose Pattern Properly
    2. Some collective info about IDisposable


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Monday, August 23, 2010 5:25 AM refine
    • Proposed as answer by Konrad Neitzel Monday, August 23, 2010 6:13 AM
    • Marked as answer by SamAgain Thursday, August 26, 2010 6:48 AM
    Monday, August 23, 2010 5:25 AM