I'm using .NET 4.5 on Windows 7, and might find a memory leak.
I have a TextBlock
(not TextBox
- it's not the Undo problem), which changes its value every second (CPU usage, time, etc...).
Using .NET Memory Profiler
(and by simply watching task manager) I noticed the memory keeps on growing. To be more accurate, I see more and more live instances of UnmanagedMemoryStream
(I tried GC.Collect()
which obviously didn't do anything).
我正在Windows 7上使用。net 4.5,可能会发现内存泄漏。我有一个TextBlock(不是TextBox -它不是撤消问题),它每秒钟都会改变它的值(CPU使用量,时间等等)。使用。net内存分析器(通过简单地查看任务管理器),我注意到内存一直在增长。更准确地说,我看到越来越多的UnmanagedMemoryStream的活动实例(我尝试了GC.Collect(),显然它什么也没做)。
After some tests, I've found out that this problem appears only when I'm setting the TextBlock
font to a resource font as follows:
经过一些测试,我发现只有当我将TextBlock字体设置为资源字体时才会出现这个问题,如下所示:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Control.Foreground" Value="#CCCCCC"/>
<Setter Property="FontFamily" Value="pack://application:,,,/MyUIControls;component/./Fonts/#Noto Sans"/>
</Style>
I tried updating the Text
property directly from code or via Binding, it behaves the same for both ways.
我尝试直接从代码或通过绑定更新文本属性,两种方式的行为都是一样的。
Bottom line:
When the FontFamily
is set, instances of UnmanagedMemoryStream
keep on coming (forever) each time I'm updating the text. When I don't (set the FontFamily
property), memory is stable.
(BTW, it happens when I use Label
instead of TextBlock
as well)
底线:当设置FontFamily时,UnmanagedMemoryStream的实例每次更新文本时(永远)都会出现。当我不(设置FontFamily属性)时,内存是稳定的。(顺便说一句,当我使用Label而不是TextBlock时也会发生)
It looks like a memory leak but I couldn't find any reference about it.
Any suggestions of how can it be solved?
它看起来像是内存泄漏,但我找不到任何有关它的引用。对如何解决这个问题有什么建议吗?
1 个解决方案
#1
11
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.
如果FontFamily来自嵌入式资源或相对路径,那么当它被使用时,它会泄漏UnmanagedMemoryStreams。当FontFamily来自系统字体或绝对路径时,它不会泄漏。
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.
工作区:用于资源字体:将字体保存到临时文件夹中,并使用存储字体的绝对路径。对于相对路径字体:解析并使用绝对路径。
#1
11
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.
如果FontFamily来自嵌入式资源或相对路径,那么当它被使用时,它会泄漏UnmanagedMemoryStreams。当FontFamily来自系统字体或绝对路径时,它不会泄漏。
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.
工作区:用于资源字体:将字体保存到临时文件夹中,并使用存储字体的绝对路径。对于相对路径字体:解析并使用绝对路径。