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.