Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an application I'm working on that opens files into a list view and lets the user modify them and other such tasks, the files are stored as archives and comprised of multiple other files. So each of these files is turned into an object with refferences to the files (also turned into objects) that make up the archive. The problem I'm having is that when the user removes the file from the interface the garbage collector doesn't seem to release all the memory the file was originally using. This is probably confusing (I know I'm confused after reading it) so here's basically what I have:
C#
class mainFileItem:ListViewItem
{
    private mainObject main;
    //Bunch of other stuff
    
    public void Delete()
    {
        this.Remove();
        this.main.Dispose();
    }
}

class mainObject:IDisposable
{
    List<subObject> subObjects;
    //Other stuff
    public void Dispose()
    {
        foreach(subObject sub in this.subObjects)
        {
            sub.Dispose();
        }
    }
}

class subObject:Idisposable
{
    private List<Resource> resources;
    private Object someObject;
    private sting someString;

    public void Dispose()
    {
        this.someObject.Dispose();
        this.someString = null;
        foreach(Resource something in resources)
        {
            something.Dispose();
        }
    }
}

class Resource:IDisposable
{
    byte[] data; //Very large, contains the resource file as raw data
    
    public void Dispose()
    {
        this.data = null;
    }
}


This is a close approximation to what my code looks like. What I'm finding is that when I call "Delete()" in "mainFileItem" not all of the memory is released (when I load the file originally memory use increases by ~20K but after "Delete()" memory use only drops ~2k). My guess is that when I call "Dispose()" in Resource "data" is not released correctly (but that's just my guess).

Additional Notes:
Whenever I call "Dispose()" in any class I set every member variable to null or call "Dispose()" on it.
Posted

1 solution

If you're looking in TaskManager to show you the amount of memory your app is using, you're looking at the wrong thing. TaskManager is showing you the memory that is RESERVED for your app by the .NET CLR, NOT how much memory your app is actually using. For that, use Performance Monitor and the .NET Memory counters or some other profiling tool.
 
Share this answer
 
Comments
amura.cxg 29-Dec-11 13:39pm    
Interesting, I didn't know that (obviously).I ended up using this tool http://www.jetbrains.com/profiler/ (for anyone in the future that has a similar problem). Thanks Dave you rock :)
TimGameDev 29-Dec-11 14:14pm    
My 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900