FlowDocument Issue with Freeing ResourcesOVERVIEW:<br/> Hello, I have recently came across an issue that has proven to be out of my league. I have narrowed it down to being something with a FlowDocument and/or TextRange. Basically, when I load an RTF file into a FlowDocument via TextRange.Load() it will not release all the resources (or whatever it is called) after all the references to these objects scope has run out. In my WPF application I load large RTF files as well as load multiple small ones repeatedly. Consequently this becomes a major issue and the memory usage for my application begins to become an overwhelming amount.<br/> <br/> QUESTION:<br/> My question is how can I fix this?<br/> <br/> TRIED METHODS:<br/> I have used used GC.Collect() as well as GC.WaitForPendingFinalizers() (recommended by someone). These work to a small degree but overall the issue still persists. I have also made sure no references are being made to anything, even the parent of the FlowDocument (FlowDocumentPageViewer).<br/> <br/> Oh and another thing I tried was both Debug and non-Debug. Overall it doesn't make any difference.<br/> <br/> TEST CODE:<br/> Here is some test code that I have to test the problem.<br/> <br/> <br/> Code Behind:<br/> <pre lang="x-c#">using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.IO; using System.Windows.Forms; namespace Testing { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void LoadRTF(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Filter = &quot;Rich Text Format (*.rtf)|*.rtf&quot;; openDlg.ShowDialog(); string path = openDlg.FileName; using (FileStream stream = new FileStream(path, FileMode.Open)) { TextRange range = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd); range.Load(stream, System.Windows.DataFormats.Rtf); } //make sure the GC collects everything GC.Collect(); GC.WaitForPendingFinalizers(); } private void OnClose(object sender, EventArgs e) { richTextBox1 = null; } } } </pre> <br/> <br/> XAML:<br/> <pre lang=x-xml>&lt;Window x:Class=&quot;Testing.Window1&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot; ShowInTaskbar=&quot;False&quot; Closed=&quot;OnClose&quot;&gt; &lt;Grid&gt; &lt;RichTextBox Name=&quot;richTextBox1&quot; Margin=&quot;0,40,0,0&quot; VerticalScrollBarVisibility=&quot;Visible&quot; /&gt; &lt;Button Name=&quot;loadButton&quot; VerticalAlignment=&quot;Top&quot; Click=&quot;LoadRTF&quot;&gt;Load RTF&lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt;</pre> <br/> This code is something I quickly threw together for testing so I'm sure it is not optimal and also some of it is completely unnescessary. I was just testing out different things.<br/> <br/> I had another window that would open up this window so that way I'd be sure that when I closed that window all the references inside it would be disposed of - at least that's what I would think...<br/> <br/> Any help is greatly apprecaited. I have been attempting to solve this for coming up a week now and have not had any luck.<br/> <br/> FINAL NOTES:<br/> I am using WPF with .NET Framework 3.5 SP1 and VS2008 Professional.<br/> I also asked this question on StackOverflow. You can find it <a title=here href="http://stackoverflow.com/questions/952985/flowdocument-memory-issue-in-c" title=here>here</a> .© 2009 Microsoft Corporation. All rights reserved.Wed, 02 Sep 2009 02:07:08 Z15e2b42e-1975-4d68-8ddb-59bbcd1f0633http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#15e2b42e-1975-4d68-8ddb-59bbcd1f0633http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#15e2b42e-1975-4d68-8ddb-59bbcd1f0633Jasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesOVERVIEW:<br/> Hello, I have recently came across an issue that has proven to be out of my league. I have narrowed it down to being something with a FlowDocument and/or TextRange. Basically, when I load an RTF file into a FlowDocument via TextRange.Load() it will not release all the resources (or whatever it is called) after all the references to these objects scope has run out. In my WPF application I load large RTF files as well as load multiple small ones repeatedly. Consequently this becomes a major issue and the memory usage for my application begins to become an overwhelming amount.<br/> <br/> QUESTION:<br/> My question is how can I fix this?<br/> <br/> TRIED METHODS:<br/> I have used used GC.Collect() as well as GC.WaitForPendingFinalizers() (recommended by someone). These work to a small degree but overall the issue still persists. I have also made sure no references are being made to anything, even the parent of the FlowDocument (FlowDocumentPageViewer).<br/> <br/> Oh and another thing I tried was both Debug and non-Debug. Overall it doesn't make any difference.<br/> <br/> TEST CODE:<br/> Here is some test code that I have to test the problem.<br/> <br/> <br/> Code Behind:<br/> <pre lang="x-c#">using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.IO; using System.Windows.Forms; namespace Testing { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void LoadRTF(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Filter = &quot;Rich Text Format (*.rtf)|*.rtf&quot;; openDlg.ShowDialog(); string path = openDlg.FileName; using (FileStream stream = new FileStream(path, FileMode.Open)) { TextRange range = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd); range.Load(stream, System.Windows.DataFormats.Rtf); } //make sure the GC collects everything GC.Collect(); GC.WaitForPendingFinalizers(); } private void OnClose(object sender, EventArgs e) { richTextBox1 = null; } } } </pre> <br/> <br/> XAML:<br/> <pre lang=x-xml>&lt;Window x:Class=&quot;Testing.Window1&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot; ShowInTaskbar=&quot;False&quot; Closed=&quot;OnClose&quot;&gt; &lt;Grid&gt; &lt;RichTextBox Name=&quot;richTextBox1&quot; Margin=&quot;0,40,0,0&quot; VerticalScrollBarVisibility=&quot;Visible&quot; /&gt; &lt;Button Name=&quot;loadButton&quot; VerticalAlignment=&quot;Top&quot; Click=&quot;LoadRTF&quot;&gt;Load RTF&lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt;</pre> <br/> This code is something I quickly threw together for testing so I'm sure it is not optimal and also some of it is completely unnescessary. I was just testing out different things.<br/> <br/> I had another window that would open up this window so that way I'd be sure that when I closed that window all the references inside it would be disposed of - at least that's what I would think...<br/> <br/> Any help is greatly apprecaited. I have been attempting to solve this for coming up a week now and have not had any luck.<br/> <br/> FINAL NOTES:<br/> I am using WPF with .NET Framework 3.5 SP1 and VS2008 Professional.<br/> I also asked this question on StackOverflow. You can find it <a title=here href="http://stackoverflow.com/questions/952985/flowdocument-memory-issue-in-c" title=here>here</a> .Tue, 09 Jun 2009 07:48:00 Z2009-06-09T07:53:47Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#acea3193-95fe-4a7b-9fa6-c43db85f0e2fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#acea3193-95fe-4a7b-9fa6-c43db85f0e2fHrach MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Hrach%20MSFTFlowDocument Issue with Freeing ResourcesHi Jasson<br/>Usually when resources don't get freed it means not all references to them were cleaned. You'd need to do some memory usage analysis to find the problem.Thu, 11 Jun 2009 23:34:02 Z2009-06-11T23:34:02Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#73c577d2-a8d1-4d12-8144-853d3943e800http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#73c577d2-a8d1-4d12-8144-853d3943e800Jasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesI have tried this before but didn't come across anything. I just tried it again right now and still didn't really come across anything. I am unsure what I am really looking for.<br/> I think that this is a fairly common problem given that it seems that just using TextRange.Load() causes it. I am still working on this...<br/> <br/> Thanks,<br/> ~JassonFri, 12 Jun 2009 10:29:32 Z2009-06-12T10:36:55Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#1cfa3086-639a-4c1b-809a-01ce0b999b30http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#1cfa3086-639a-4c1b-809a-01ce0b999b30xalnixhttp://social.msdn.microsoft.com/Profile/en-US/?user=xalnixFlowDocument Issue with Freeing ResourcesWhat is the &quot;UndoLimit&quot; on the RichTextBox (default is -1 which allows the undo queue to use all available memory).  Each time you load a new file into the RichTextBox, information is being saved to &quot;undo&quot; the edit (load).  Depending on the limit, the amount of apparent memory leak could add up. <hr class=sig> Les Potter, Xalnix Corporation, <a href="http://yacsharpblog.blogspot.com">Yet Another C# Blog</a>Fri, 12 Jun 2009 11:32:48 Z2009-06-12T11:44:27Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#d13aefb1-477f-48ea-8012-b76cbb5a0e63http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#d13aefb1-477f-48ea-8012-b76cbb5a0e63xalnixhttp://social.msdn.microsoft.com/Profile/en-US/?user=xalnixFlowDocument Issue with Freeing ResourcesTextRange.Load() and Undo do not cooperate very well, you should probably just turn it off before the load.  Turn it back on after the load.<hr class="sig">Les Potter, Xalnix Corporation, <a href="http://yacsharpblog.blogspot.com">Yet Another C# Blog</a>Fri, 12 Jun 2009 12:02:26 Z2009-06-12T12:02:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#eb4c3c2b-9b66-4939-a43f-4b8ee4a511a2http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#eb4c3c2b-9b66-4939-a43f-4b8ee4a511a2xalnixhttp://social.msdn.microsoft.com/Profile/en-US/?user=xalnixFlowDocument Issue with Freeing Resources<pre lang="x-c#"> using (FileStream stream = new FileStream(path, FileMode.Open)) { flowdoc = new FlowDocument(); flow.Document = flowdoc; TextRange range = new TextRange(flowdoc.ContentStart, flowdoc.ContentEnd); range.Load(stream, System.Windows.DataFormats.Rtf); } </pre> Sorry, I lost track that this was about FlowDocument.  So, try allocating a new flow document and replacing the old.<br/><br/> <pre lang=x-xml> &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height=&quot;20&quot;/&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row=&quot;0&quot; Click=&quot;LoadRTF&quot;&gt;Clickme&lt;/Button&gt; &lt;FlowDocumentPageViewer Grid.Row=&quot;1&quot; Name=&quot;flow&quot; &gt; &lt;FlowDocument Name=&quot;flowdoc&quot;&gt;&lt;/FlowDocument&gt; &lt;/FlowDocumentPageViewer&gt; &lt;/Grid&gt; </pre> <br/><hr class="sig">Les Potter, Xalnix Corporation, <a href="http://yacsharpblog.blogspot.com">Yet Another C# Blog</a>Fri, 12 Jun 2009 12:48:37 Z2009-06-12T12:48:37Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#3f21b597-1a79-4252-bac7-0e285f0659b3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#3f21b597-1a79-4252-bac7-0e285f0659b3Jasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesChanging the RichTextBox Undo isn't doing anything to the memory at least it is not the problem.<br/> <br/> I also tried your second suggestion but used a RichTextBox instead of a FlowDocumentPageViewer since that is what I am wanting to use in my application. This as well does not seem to have any affect on the memory. Thank you for the suggestion.<br/> <br/> ~JassonFri, 12 Jun 2009 18:19:21 Z2009-06-12T18:19:21Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#d4c61bd8-1d17-418a-b815-164ae80146fehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#d4c61bd8-1d17-418a-b815-164ae80146feJasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesHello, I hate to bump this question but I am still looking for a solution. Any help is greatly appreciated. :)Sat, 20 Jun 2009 08:46:56 Z2009-06-20T08:46:56Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#4ef3abdf-6ae5-47a1-83a2-d070c8685d86http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#4ef3abdf-6ae5-47a1-83a2-d070c8685d86Hrach MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Hrach%20MSFTFlowDocument Issue with Freeing ResourcesI'd suggest to investigate this issues under deubgger. Please have a look at <a href="http://msdn.microsoft.com/en-us/library/bb190764.aspx"><strong><span style="color:#0033cc">DumpHeap</span></strong></a> related suggestion on this thread - <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2245ee83-6f51-4095-9ed2-d82bbeda61a8">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2245ee83-6f51-4095-9ed2-d82bbeda61a8</a><br/>Let me know how it goes.<br/>On a side note - closing the first window does not guarantee that all resources it referenced will be collected, since they still may be referenced from other places, depending on the logic of application.Sat, 20 Jun 2009 18:05:09 Z2009-06-20T18:05:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#4130d451-b401-4a1d-8786-a0005ab98f5fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#4130d451-b401-4a1d-8786-a0005ab98f5fJasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesAlright. I made an even simpler project to test this DumpHeap suggestion. This simple project only has a button inside the window. This is the click event for the button.<br/> <br/> <pre lang="x-c#"> private void Click_Button(object sender, EventArgs e) { RichTextBox b = new RichTextBox(); FlowDocument f = new FlowDocument(); }</pre> Using WinDbg, I clicked on the button a few (4) times and then checked the heap for both RichTextBox and FlowDocument types. Here is my log.<br/> <br/> <br/> 0:007&gt; .loadby sos mscorwks<br/> 0:007&gt; !DumpHeap -type System.Windows.Controls.RichTextBox<br/>  Address       MT     Size<br/> 013817c4 55cdc0a0      280     <br/> 013cfbcc 55cdc0a0      280     <br/> 013dba0c 55cdc0a0      280     <br/> 013e7864 55cdc0a0      280     <br/> total 4 objects<br/> Statistics:<br/>       MT    Count    TotalSize Class Name<br/> 55cdc0a0        4         1120 System.Windows.Controls.RichTextBox<br/> Total 4 objects<br/> 0:007&gt; !DumpHeap -type System.Windows.Documents.FlowDocument<br/>  Address       MT     Size<br/> 013b7450 55cd21d8       84     <br/> 013c6ec4 55cd21d8       84     <br/> 013cfd4c 55cd21d8       84     <br/> 013d3204 55cd21d8       84     <br/> 013dbb8c 55cd21d8       84     <br/> 013df054 55cd21d8       84     <br/> 013e79e4 55cd21d8       84     <br/> 013eae7c 55cd21d8       84     <br/> total 8 objects<br/> Statistics:<br/>       MT    Count    TotalSize Class Name<br/> 55cd21d8        8          672 System.Windows.Documents.FlowDocument<br/> Total 8 objects<br/> <br/> <br/> I dont know if I am doing this correctly since I have never used WinDbg before, but if I am right it looks like both of those are not being released because they are being held by reference somewhere else. But where? I am not sure how how I should use this information to investigate further.<br/> <br/> So my new questions are what is holding onto these and how can I go about releasing them? From what I understand this is a problem everyone has to deal with (or avoid). Thanks in advance :)Sun, 21 Jun 2009 06:41:04 Z2009-06-21T06:41:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#abc55e0b-64bd-4a05-a578-5e398c603d10http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#abc55e0b-64bd-4a05-a578-5e398c603d10Hrach MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Hrach%20MSFTFlowDocument Issue with Freeing ResourcesInteresting, how could there be 8 FlowDocuments if only 4 new operators were called:)<br/>Is the Click_Button the only added code in application? Can you share whole repro app. solution to debug it?Sun, 21 Jun 2009 19:40:38 Z2009-06-21T19:40:38Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#b182fb10-2915-422c-b94e-2942a28b7448http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#b182fb10-2915-422c-b94e-2942a28b7448Jasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesIf RichTextBox is not provided a FlowDocument when it is created then it will make its own. That is why there are twice as many FlowDocuments - this was expected this.<br/> <br/> Here is all of the code in the project.<br/> <br/> XAML<br/> <pre lang=x-xml>&lt;Window x:Class=&quot;WpfApplication2.Window1&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt; &lt;Grid&gt; &lt;Button Click=&quot;Click_Button&quot;&gt;Click&lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt; </pre> <br/> Code Behind<br/> <pre lang="x-c#">using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace WpfApplication2 { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Click_Button(object sender, EventArgs e) { RichTextBox b = new RichTextBox(); FlowDocument f = new FlowDocument(); } } } </pre> <br/> That is all I added. Like I said, this is a really simple program but still has the problem. Try clicking on the button several times and watch the memory usage for this application go up and up. Thanks Hrach and good luck on your debugging attempt :)Sun, 21 Jun 2009 19:58:15 Z2009-06-21T19:58:15Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#b36e7ec7-5e11-4208-a1a1-0f9c96320a69http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#b36e7ec7-5e11-4208-a1a1-0f9c96320a69Tysonkbhttp://social.msdn.microsoft.com/Profile/en-US/?user=TysonkbFlowDocument Issue with Freeing ResourcesI'm getting the same problem... I read a 140mb .rtf file into a TextRange, and memory sits at 240mb :( anyone solved this one yet?Tue, 30 Jun 2009 05:01:23 Z2009-06-30T05:01:23Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#f8d5e00e-4c5e-4c66-b406-a0b20b6629cdhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#f8d5e00e-4c5e-4c66-b406-a0b20b6629cdJasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesI have made a little progress in solving this but am still stuck. Here is a post I had on another site about this problem.<br/> <br/> <br/> It was the first time I used Windbg and so I didn't know what to do with the address to find the references. Here is what I got.<br/> <br/> <br/> <br/>      Address       MT     Size<br/>     0131c9c0 55cd21d8       84     <br/>     013479e0 55cd21d8       84     <br/>     044dabe0 55cd21d8       84     <br/>     total 3 objects<br/>     Statistics:<br/>           MT    Count    TotalSize Class Name<br/>     55cd21d8        3          252 System.Windows.Documents.FlowDocument<br/>     Total 3 objects<br/>     0:011&gt; !gcroot 0131c9c0<br/>     Note: Roots found on stacks may be false positives. Run &quot;!help gcroot&quot; for<br/>     more info.<br/>     Scan Thread 0 OSTHread 47c<br/>     Scan Thread 2 OSTHread be8<br/>     Scan Thread 4 OSTHread 498<br/>     DOMAIN(001657B0):HANDLE(WeakSh):911788:Root:0131ff98(System.EventHandler)-&gt;<br/>     0131fcd4(System.Windows.Documents.AdornerLayer)-&gt;<br/>     012fad68(MemoryTesting.Window2)-&gt;<br/>     0131c9c0(System.Windows.Documents.FlowDocument)<br/>     DOMAIN(001657B0):HANDLE(WeakSh):911cb0:Root:0131ca90(MS.Internal.PtsHost.PtsContext)-&gt;<br/>     0131cb14(MS.Internal.PtsHost.PtsContext+HandleIndex[])-&gt;<br/>     0133d668(MS.Internal.PtsHost.TextParagraph)-&gt;<br/>     0131c9c0(System.Windows.Documents.FlowDocument)<br/>     DOMAIN(001657B0):HANDLE(WeakSh):9124a8:Root:01320a2c(MS.Internal.PtsHost.FlowDocumentPage)-&gt;<br/>     0133d5d0(System.Windows.Documents.TextPointer)-&gt;<br/>     0131ca14(System.Windows.Documents.TextContainer)-&gt;<br/>     0131c9c0(System.Windows.Documents.FlowDocument)<br/> <br/> <br/> This is after I closed the window. So it looks like it is being referenced by a few things. Now that I know this <strong>how do I go about freeing up these references so they can release the FlowDocument?</strong>Wed, 01 Jul 2009 00:22:46 Z2009-07-01T00:22:46Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#43239629-13c5-4436-8926-00094a92bf2bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#43239629-13c5-4436-8926-00094a92bf2bBen Westbrook - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Ben%20Westbrook%20-%20MSFTFlowDocument Issue with Freeing ResourcesAll the references above are weak references, so they shouldn't factor into keeping the FlowDocuments in memory.  Has the GC actually run yet?  If memory pressure is low, it won't be in any rush.<br/><br/>Two things in general that I can think of that might be at fault here, aside from an outright bug where you're accidently holding a reference directly or indirectly.<br/><br/>1. RichTextBox sometimes BeginInvokes a Dispatcher delegate to do some delayed initialization.  Your test app will need to run the Dispatcher queue (Dispatcher.Run) to free this reference.  If you're running with a default Application and just adding a handler for a Button press, this is already happening automatically.<br/><br/>2. The GC won't actually run until it detects sufficient memory pressure.  Because RichTextBox has a finalizer, you need to be careful in how you force the collection:<br/><br/>GC.Collect();<br/>GC.WaitingForPendingFinalizers();<br/>GC.Collect();<br/><br/>Ben<br/>Wed, 01 Jul 2009 16:29:09 Z2009-07-01T16:29:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#5f5b5ce9-6a7f-42ac-a618-faba80cd1186http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#5f5b5ce9-6a7f-42ac-a618-faba80cd1186Jasson Mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jasson%20MFlowDocument Issue with Freeing ResourcesHey Ben, I am running a default application and just adding in a handler for the button press so that should be taken care of automagically I believe. As for #2, I have done this and still have the issue.Thu, 02 Jul 2009 22:55:32 Z2009-07-02T22:55:32Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#e14f9ce4-ef7d-4f47-90a5-5d5e4ffc8c24http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/15e2b42e-1975-4d68-8ddb-59bbcd1f0633#e14f9ce4-ef7d-4f47-90a5-5d5e4ffc8c24ICCOhttp://social.msdn.microsoft.com/Profile/en-US/?user=ICCOFlowDocument Issue with Freeing ResourcesI have the same problem.Wed, 02 Sep 2009 02:07:08 Z2009-09-02T02:07:08Z