Solved

Freeing Memory, Dispose, Class Objects

Posted on 2011-02-15
5
614 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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