Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Freeing Memory, Dispose, Class Objects

Posted on 2011-02-15
5
Medium Priority
?
644 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 45

Accepted Solution

by:
AndyAinscow earned 500 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 500 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 500 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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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