Memory leaks problems  
Author Message
Slarty





PostPosted: Thu Dec 04 13:27:18 CST 2003 Top

Visual C#.Net >> Memory leaks problems

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

DotNet186  
 
 
Ignacio





PostPosted: Thu Dec 04 13:27:18 CST 2003 Top

Visual C#.Net >> Memory leaks problems 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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



> 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
>
>