Q: Release a class from memory (NewBee quiz)

M

Martin

Hi!

I have a class like this

Class MyClass
{
Public MyClass()
{
// Doing stuff
}

private string myVar;

and a lot of more code.

}

Now in my Main program i declare this line

private MyClass myClass;

and in a procedure i do the folowing

private void dotest()
{
myClass = new MyClass();
}

Now the myClass is in memory att all time.

When i am thru with the myClass how do "disintegrate" the class, that is
remove it from memory,
releasign resourses?

Regards
Martin
 
P

Peter Duniho

Martin said:
[...]
When i am thru with the myClass how do "disintegrate" the class, that is
remove it from memory, releasign resourses?

It's unusual, but not unheard of, to have a class allocated and stored
in some static field when it's only going to be needed some of the time
and not others. In those situations, you can simply set the variable
storing the reference to null when you no longer need the class. As
soon as you do that, the instance becomes eligible for garbage
collection, and the memory will be reclaimed when it's needed, if not
sooner.

Keeping in mind to follow the conventions for calling
IDisposable.Dispose() when necessary before setting the variable to
null, of course. This may or may not apply, depending on whether
MyClass implements IDisposable.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top