Memory leaks problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Emmanuel Schnetzler

    #1

    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.Suppr essFinalize(thi s);
    }
    }

    The problem is that the memory is not freed when I dispose an instance of
    this class:
    Variable v=new Variable(800000 0);
    ....
    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


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

    #2
    Re: 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

    "Emmanuel Schnetzler" <marsu@earthlin g.net> wrote in message
    news:hTKzb.3253 8$bU.31632@news svr29.news.prod igy.com...[color=blue]
    > 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[/color]
    my[color=blue]
    > 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.Suppr essFinalize(thi s);
    > }
    > }
    >
    > The problem is that the memory is not freed when I dispose an instance of
    > this class:
    > Variable v=new Variable(800000 0);
    > ...
    > v.Dispose();
    > v=null;
    >
    > At this point, when I look at the memory used, it is the same as right[/color]
    after[color=blue]
    > the object creation. Forcing garbage collection doesn't help. The object[/color]
    is[color=blue]
    > not referenced anywhere else. Am I missing something?
    >
    > Any help appreciated!
    >
    > Manu
    >
    >[/color]


    Comment

    Working...