Solved

Freeing Memory, Dispose, Class Objects

Posted on 2011-02-15
5
585 Views
Last Modified: 2013-12-17
Experts,

What is the best way to clear an object of a class? Do I have to clear it from the Memory too? Should I be implementing Dispose()? If so - how?

Lets say I have a simple class. ClassSimple.cs. It contains a few properties and methods. I create an object of this class - CSimpleObj. I use it in the code and at some point I no longer need it. Lets say at a different point I need to use this but I don`t want any of the values from the previous instance. Do I do just a CSimpleObj = null or should I be actually clearing the object from the memory?

How would this effect the performance? If don`t do a cleanup.

Example.
//SimpleClass.cs
  class SimpleClass
    {
        private int testInt;
        public int TestInt
        {
            get
            {
                return testInt;
            }
            set
            {
                testInt = value;
            }
        }
    }

//Form.cs
  SimpleClass CSimpleObj = new SimpleClass();

        private void button1_Click(object sender, EventArgs e)
        {
            CSimpleObj.TestInt = 45;
            //Do something with CSimpleObj
            //Lets say, I no longer need the object.
            //I want to clear the object
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //I want an empty object here
            //Do something else here
        }

0
Comment
Question by:San24
5 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 125 total points
ID: 34900191
Just set it to null, the garbage collector will perform the memory cleanup for you.

(Dispose is the place to free up any resources the class uses)
0
 
LVL 18

Assisted Solution

by:Anil Golamari
Anil Golamari earned 125 total points
ID: 34900570
One's you are out of that class Grabage collector will try to clean up all objects that are created, but if you want to do call grabage collector to clean the objects that are created you can invoke garbage collector.

http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
0
 
LVL 9

Assisted Solution

by:rawinnlnx9
rawinnlnx9 earned 125 total points
ID: 34900675
This is a good discussion of the topic's merrits and such: http://blogs.msdn.com/b/ricom/archive/2003/12/02/40780.aspx

The discussion doesn't enter into how memory is allocated, stored and released like the referenced magazine article. Instead it stays out of the raw internals and provides more of a why and why not for garbage collection. So between the MSDN article and this link you should exhaustively understand memory in C# and .Net Framework.
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 125 total points
ID: 34902108
Dispose method performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. So, we are talking about UNMANAGED" resources. For your example there is no sense to implement Dispose.

See also:
http://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.71).aspx

and

http://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.71).aspx

 The rest is answered by AndyAinscow

0
 

Author Closing Comment

by:San24
ID: 34906868
Thanks everyone! Very helpful.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now