Solved

Freeing Memory, Dispose, Class Objects

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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