2

NOTE: The Question could be duplicate of this, still posting it to get the update on this issue.

I am creating multiple (more than 1000k, not in loop but as and when required) objects of flow document in a component and the application was crashing due to out of memory exception.After using .NET profiler I found that multiple objects were still in memory.

I created a test application for simulation to get the details. I found that the objects were in memory after the usage (WinDbg helped me in identifying this). In short if 5000 objects of FlowDocument are created, even after calling GC.Collect after the interval of 1 second, I found that ~600MB is still allocated for the application. The memory is released only after closing the application.

Has anyone found the solution to clear the memory allocated to FlowDocument?

The Code is as follows

private void CreateObjects()
{

    for (int index = 0; index < 5000; index++)
    {
        FlowDocument fd = new FlowDocument();
        //Opacity is accessed just to use any property of object. It does not have any significance.
        var ff = fd.Foreground.Opacity;
    }


}

As mentioned above, I am also calling GC.Collect after 1 second to free the memory. If I dont call GC.Collect, ~1.2 GB memory gets allocated to the application.

Community
  • 1
  • 1
Ram
  • 11,404
  • 15
  • 62
  • 93
  • 2
    Narrow the problem down to a sample that reproduces the leak and post it so we can see the code. – Richard Harrison Jan 21 '13 at 14:21
  • @RichardHarrison : The code is posted for your reference. – Ram Jan 21 '13 at 19:24
  • If you have already used WinDbg to investigate this, then you should find out what is keeping those FlowDocuments from being collected. Use the `!gcroot` command. – Brian Rasmussen Jan 22 '13 at 06:48
  • @BrianRasmussen: WinDbg shows the test application holds the reference of the FlowDocument. However What I expect that the references should get released when Dispose is called. – Ram Jan 22 '13 at 07:16

2 Answers2

2

In the question you have stated that you are employing WinDbg. using WinDbg, you can find references of these objects and try to remove them in the code. refer to diagnose a .net memory leak

Also, Flow Document objects or properties of the flow document objects may be using unmanaged resources which shall be freed by implementing IDisposable interface; for further advice you need to share the code.

daryal
  • 14,643
  • 4
  • 38
  • 54
0

Try not assigning the variable each loop

private void CreateObjects()
{
    FlowDocument fd;
    for (int index = 0; index < 5000; index++)
    {
        fd = new FlowDocument();
        //Opacity is accessed just to use any property of object. It does not have any significance.
        var ff = fd.Foreground.Opacity;
    }
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176