FlowDocument的内存问题在C#内存、问题、FlowDocument

2023-09-07 02:27:33 作者:陌上人如玉

我目前正在试图解决一个问题,释放的FlowDocument资源。我加载RTF文件,并​​把它与TextRange.Load一个的FlowDocument。我注意到,它这样做后,它就会保存这些资源和GC没有收集。我已经跑了内存分析器,并已经看到,这是事实。我也把范围缩小到是我加载居然把该RTF到的FlowDocument。如果我不这样做,那么一切都OK。所以我知道这是问题。

I am currently attempting to deal with an issue with releasing a FlowDocument resources. I am loading an rtf file and putting it into a FlowDocument with TextRange.Load. I noticed that after it does this it holds onto those resources and GC doesn't collect it. I have ran a memory profiler and have seen that this is true. I have also narrowed it down to were I load actually put the rtf into the FlowDocument. If I dont do that, then everything is ok. So I know this is the issue.

我希望一些指导一下我怎么能解决这个问题。这里是code加载的RTF和一切。我有评论其他所有code了,甚至把它放在自己的范围,以及试图GC.Collect的()。任何帮助是极大的AP preciated。

I am hoping for some guidance to what how I can solve this problem. Here is the code that loads the rtf and everything. I have commented all of the other code out and even put it in its own scope as well as tried GC.Collect(). Any help is greatly appreciated.

编辑: 这是我的code完全的时刻。我已经采取了一切,除了最基本的要素得到它运行。这个问题仍然存在。正如你所看到的FlowDocument和TextRange的不引用任何其他地方。

Here is my code in full at the moment. I have taken out everything else except for the bare essentials to get it running. The problem is still there. As you can see the FlowDocument and TextRange are not referenced anywhere else.

    public LoadRTFWindow(string file)
    {
        InitializeComponent();

        using (FileStream reader = new FileStream(file, FileMode.Open))
        {
            FlowDocument doc = new FlowDocument();
            TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
            range.Load(reader, System.Windows.DataFormats.Rtf);
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

我发现this帖子,我希望能帮助我解决我的问题,但我没有运气吧。任何形式的帮助是很大的AP preciated。谢谢你。

I found this post, which I was hoping would help me solve my problem but I had no luck with it. Any type of help is greatly appreciated. Thank you.

编辑:我想我应该提到我检查这个的主要途径。我有Windows任务管理器打开,我看我的应用程序的进程正在使用的内存使用情况。当我运行上面的code中的应用去从40,000K至70,000K,而这样做的TextRange.Load()(这是一个大的400页RTF),一次是完成它下降到61,000K,并在那里停留。我的期望是,它会下降到40,000K或至少非常接近回落到了。

I figure I should mention the major way I am checking this. I have Windows Task Manager open and am watching the memory usage my application's process is using. When I run the above code the application goes from 40,000K to 70,000K while doing the TextRange.Load() (this is a large 400 page RTF) and once that finishes it drops down to 61,000K and stays there. My expectation is that it would drop back down to 40,000K or at least very close to it.

正如我前面提到的我用了一个内存分析器,看到有款,Run..ect地段。对象还活着之后。

As I mentioned earlier I used a memory profiler and saw that there were LOTS of Paragraph, Run..ect. objects still Alive afterwards.

推荐答案

如果我已经证实,有内存泄漏,这里就是我会做调试问题。

If I've confirmed that there's a memory leak, here's what I would do to debug the problem.

在安装调试工具的Windows从http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx#a 从安装目录中火起来的Windbg。 在启动应用程序,并做内存泄漏的操作。 连接WinDbg到您的应用程序(F6)。 键入 .loadby索斯mscorwks 键入!dumpheap型的FlowDocument 检查上述命令的结果。如果看到多个FlowDocuments,为第一列的每一个值(其中包含的地址),执行 Install Debugging Tools for Windows from http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx#a Fire up Windbg from the installation directory. Launch your application and do the operations that leak memory. Attach Windbg to your application (F6). Type .loadby sos mscorwks Type !dumpheap -type FlowDocument Check the result of the above command. If you see multiple FlowDocuments, for each value of the first column (which contains the address), do

键入 gcroot<!第一列的制造&gt价值;

这应该告诉你谁在持有的参考。

That should show you who's holding on to the reference.