Solved

Freeing Memory, Dispose, Class Objects

Posted on 2011-02-15
5
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 30

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

710 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