Wpf Textblock Memory Leak When Using Font

WPF TextBlock memory leak when using Font

A FontFamily leaks UnmanagedMemoryStreams when it is used if it was sourced from an embedded resource or a relative path. When the FontFamily is sourced from a system font or absolute path, it does not leak.

You can look here and download the project that reproduces the problem.

Workaround: For Resource fonts: save fonts into a temporary folder and use the absolute path to the stored font.
For relative path fonts: resolve and use the absolute path instead.

TextBox.Text Leaking Memory in WPF Application

From the documentation of the TextBoxBase.UndoLimit property:

The number of actions stored in the undo queue. The default is –1, which
means the undo queue is limited to the
memory that is available.

You found that limit. Set it to a reasonably small value.

It otherwise doesn't normally make a lot of sense to display logging information in a TextBox. It is really meant to allow the user to enter text. Perhaps TextBlock is a better choice.


UPDATE: in .NET 4.5, the default of -1 was changed to 100 to avoid this kind of runaway memory usage.

WPF load font from Resources

There's a memory leak you need to be aware of.

Any font reference which uses a relative path will cause a memory leak.
The way to do this is to use an absolute path.
And yes, that is a nuisance.

See this:
WPF TextBlock memory leak when using Font

I've recently been working on something which uses fancy fonts and investigated the issue. It's still there with .net 4.7.
I wouldn't use a temporary folder, deliver your ttf to the same folder as your exe or into local appdata if you have several apps which will use the same ttf.

My plan, when I get round to this specific aspect of our app, is to write a custom markup extension which will allow me to pass a short name and go find the absolute path on the user's machine. I'll be using appdata.

Your immediate problem is because you're not using a relative path. Put a / in front of your path there.

Value="/Resources/#Abstract"

But you will then have a memory leak.

WPF Glyphs control font uri!(its slow and remains in memory)

This is actually the same problem as here https://stackoverflow.com/a/31452979 and it's not going to be fixed anytime soon as following answer was made on 2013.10.01:

The WPF team has recently reviewed this issue and will not be addressing this issue as at this time the team is focusing on the bugs impacting the highest number of WPF developers. If you believe that this was resolved in error, please reactivate this bug with any necessary supporting details.

We appreciate the feedback. However, this issue will not be addressed in the next version of WPF. Thank you.
–WPF Team.

As both these cases are caused by UnmanagedMemoryStreams for now you can only make temporary folder and save font into it. It is not pretty but is all what we have.

What causes UnmanagedMemoryStream in WPF (Memory Leak)?

Like said in the comments, FontFamily is causing the leak. More details:
WPF TextBlock memory leak when using Font



Related Topics



Leave a reply



Submit