SteveHayter

Hi all,I have a RichTextBox that is loaded with a FlowDocument, and then placed onto canvas at a specfic location.

Now, memory usage up to creating the RichTextBox is steady, and the garbage collector seems to work well.

However, when I create a RichTextBox, and set its Document property to the FlowDocument, the memory consumed jumps by about 1.5 - 2.5 MB, and never seems to get garbage collected. Since we perform this operation many times over (to put numerous RichTextBoxs on a canvas in different locations), it makes memory usage substantial (especially as it never goes down).

I've tried clearing the FlowDocument blocks, etc - but it makes no difference.

Can anyone maybe explain why this is happening

My code is below:

Code Snippet

protected void Generate()
{
Canvas canvas = this.GenerateCanvas();

FlowDocument flowDocument = this.GenerateFlowDocument();

if (flowDocument != null)
{
RichTextBox richTextBox = this.GenerateRichTextBox(flowDocument);
// Memory jumps here. We do other things with the RichTextBox, but they are all commented out in my example. No other operations are carried out after here.

}

protected Canvas GenerateCanvas()
{
Canvas canvas = new Canvas();

BitmapImage image = new BitmapImage(new Uri(this.backgroundPath, UriKind.Absolute));

ImageBrush brush = new ImageBrush(image);

canvas.Background = brush;
canvas.Width = image.Width;
canvas.Height = image.Height;

return canvas;
}

protected FlowDocument GenerateFlowDocument()
{
FlowDocument flowDocument = null;

StringReader stringReader = new StringReader(this.xaml.OuterXml);
XmlReader xmlReader = new XmlTextReader(stringReader);

object xamlObject = XamlReader.Load(xmlReader);

if (xamlObject.GetType() == typeof(FlowDocument))
{
flowDocument = (FlowDocument)xamlObject;
}

return flowDocument;
}

protected RichTextBox GenerateRichTextBox(FlowDocument flowDocument)
{
RichTextBox richTextBox = new RichTextBox();

richTextBox.Background = Brushes.Transparent;
richTextBox.Width = 200; // width + height
richTextBox.Height = 200;
richTextBox.BorderThickness = new Thickness(0);
richTextBox.Margin = new Thickness(100, 85, 0, 0); // x-y co-ordinates

richTextBox.Document = flowDocument;

richTextBox.RenderTransform = new RotateTransform(-45, richTextBox.Width / 2, richTextBox.Height / 2);

return richTextBox;
}




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

Should point out that if the RichTextBox line is commented out, memory consumption is okay.




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

Siri Vellanki - MSFT

Can you try again after setting RichTextBox.IsUndoEnabled = false for all the RTB's you create to see if its the UndoStack which is holding the memory.




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

Doesn't seem to make any difference. I've set it to false immediately after constructing it and setting the document. Memory still just jumps up.

If useful to anybody, the task manager output is below.

I simply have a button which executes the Generate method each time it is clicked. Each line below is a button click while the application is running.


Code Snippet

Mem Usage Handles USER Objects GDI Objects
--------- ------- ------------ -----------
33408 231 27 31
48344 314 27 31
50724 356 27 31
53032 394 27 31
56080 434 27 31
58304 473 27 31
60576 502 27 31
62904 533 27 31






Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

Peter Sarrett - MSFT

How large is the FlowDocument you create If you just create a FlowDocument without a RichTextBox, do you see a similar increase in memory consumption




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

No not at all. Without the RichTextBox, the memory remains steady.

The FlowDocument is fairly small. It contains a couple of paragraphs with 7-10 words each, and some simple formatting.

Edit: just tried it with a really simple one and makes no difference:

Code Snippet

<FlowDocument xml:space=\"preserve\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
<Paragraph>test</Paragraph>
</FlowDocument>






Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

Peter Sarrett - MSFT

We've repro'd the problem and are investigating.

In the meantime, for your scenario is it necessary to replace the document on an already-existing RichTextBox, or are you creating new RichTextBoxes each time If the latter, try using the RTB constructor that takes a FlowDocument as an argument and see if that makes a difference.






Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

Thanks Peter. Glad its been repro'd and it wasn't just me!

Each RichTextBox could potentially contain different content, so I am currently creating a new one for each piece of content. However, I could reorganise the code so the same object is just reused, and a new FlowDocument set.

Tried it with the constructor as per the code in the first post. Doesn't seem to make any difference.




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox


Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

Yeah that hasn't been any response to this (I started this thread a couple of months ago). The only way I found to regain the memory was to kill the dispatcher. I'm sure this isn't as designed, but I couldn't find another solution.

Anyone at MS had a chance to look at this




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

kk302999

Thats interesting, what do you mean by kill the dispatcher I was going to create my RTB's on worker threads. To eliminate the creation/destruction problem, I was going to use a global cache of some sort and re-use RTB's. Its a silly work-around, but the WPF RTB is just too awesome.

I built my own text editor (1 months worth so it lacks many features) using .NET's GDI+ text support, because .NET 1.0-2.0 just wrapped the unmanaged text controls. I needed rendering at any resolution, and those were not ideal. It is amazing how much we take for granted in text controls, they are crazy heavy/complex.

Kevin






Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

kk302999

Steve:

I think the answer to our question may have something to do with Rob Relyea's comment that WPF was not architected for a server scenario (which makes sense).

Rule #371 of Framework Design: Consumers do not use a framework in the way the designers intended.

*cough*registry*cough*

Kevin






Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

SteveHayter

What do you mean This behaviour occurs within a basic WPF Windows application.




Re: Windows Presentation Foundation (WPF) Memory Use - RichTextBox

Arathi Ramani

I'm not sure if this will help, but I posted a response to another question about FlowDocument memory usage here:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1919798&SiteID=1

I don't know if it's related, but it might be. Hope it helps.