How to free an object?

  • Thread starter Thread starter Visual Systems AB \(Martin Arvidsson\)
  • Start date Start date
V

Visual Systems AB \(Martin Arvidsson\)

Hi!

Every time an object is created it needs an amount of memory, but it doesn't
release the memory, when the routine is run, it takes more memory...

Code below...

public class MyFirstClass
{
public MyFirstClass
{
// Initialization done here
}
public void DoStuff()
{
...
}
}

public class myOtherClass
{
public myOtherClass
{
}

public void ExecuteStuff()
{
MyFirstClass ThisIsIt = new MyFirstClass();
ThisIsIt.DoStuff();
}
}

When i execute ExecuteStuff shouldn't ThisIsIt be released from memory?
because it seems that more memory is allocated everytime i execute it...?

Regards

Martin Arvidsson, Sweden
 
Every time an object is created it needs an amount of memory, but it doesn't
release the memory, when the routine is run, it takes more memory...

..NET uses garbage collection, so there is no easy and efficient way to free
objects ahead of the garbage collector; in fact so doing tends to slow
things down. You should not really need to free objects yourself.

If you are creating an instance of a class just to run a method, and no
parameters are passed to it and nothing is returned from it, then why are
you creating an instance ?

What does ThisIsIt.DoStuff() do ? Does ThisIsIt have internal state that is
altered ?

Joanna
 
Memory allocation and deallocation is in the hands of Garbage Collector.
It frees memory when needed and disallocates objects that are not
referenced.
You can also force GC to do collection if you need to.
System.GC.Collect();
 

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

Back
Top