Memory leaks problems

  • Thread starter Emmanuel Schnetzler
  • Start date
E

Emmanuel Schnetzler

Hi all,

this might be a very basic question but I've been struggling with the
problem for a while and can't find the solution.

I am allocating a big chunk of memory that I want to deallocate when I'm
done with it (sorry if I'm mixing C/C++ style language with C#). Here is my
class:
public class Variable: IDisposable
{
private double[] values=null;
public Variable(int n)
{
values=new double[n];
}
public virtual void Dispose()
{
values=null;
System.GC.SuppressFinalize(this);
}
}

The problem is that the memory is not freed when I dispose an instance of
this class:
Variable v=new Variable(8000000);
....
v.Dispose();
v=null;

At this point, when I look at the memory used, it is the same as right after
the object creation. Forcing garbage collection doesn't help. The object is
not referenced anywhere else. Am I missing something?

Any help appreciated!

Manu
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Emmanuel,

First of all, if you are using only managed code you don't need to free it,
the framework will do it for you, values is a managed array, therefore the
framework will collect that memory without your help, all you have to do is
set it to null.

Therefore you do not need to implement IDisposable .

What are you using to report the memory consuption?
I'm not an expert on the memory management of the framework, but it's
possible that the GC does not return that memory to the system at once, if
the system has free memory it makes sense to keep this unused memory
reserved, so in the case that you need more memory the framework does not
need to request it to the system. The GC must have some optimization for
this.

Hope this help,
 

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