C#垃圾回收问题 [英] C# Garbage Collection Issue

查看:63
本文介绍了C#垃圾回收问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的应用程序,该应用程序将文件打开到列表视图中,并允许用户修改它们和其他此类任务,这些文件存储为档案,并由多个其他文件组成.因此,将这些文件中的每一个都转换为对象,并引用构成归档文件的文件(也转换为对象).我遇到的问题是,当用户从界面中删除文件时,垃圾收集器似乎并没有释放文件最初使用的所有内存.这可能令人困惑(我知道阅读后会感到困惑),所以这基本上就是我拥有的内容:

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:

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



这与我的代码看起来非常近似.我发现的是,当我在"mainFileItem"中调用"Delete()"时,并不是所有的内存都被释放(当我加载文件时,最初的内存使用量增加了约20K,但是在"Delete()"内存之后才使用)下降〜2k).我的猜测是,当我在资源数据"中调用"Dispose()"时,释放不正确(但这只是我的猜测).

附加说明:
每当在任何类中调用"Dispose()"时,我都会将每个成员变量设置为null或在其上调用"Dispose()".



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.

推荐答案

在TaskManager中查看以显示您的应用程序正在使用的内存量,您看错了. TaskManager向您显示.NET CLR为您的应用程序保留的内存,而不是您的应用程序实际使用的内存.为此,请使用性能监视器和.NET内存计数器或某些其他性能分析工具.
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.


这篇关于C#垃圾回收问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆