问题描述
我有一个应用程序(WPF)这巨大的数字创造BitmapImages(如25000)。好像框架使用一些内部逻辑,所以在创建后有大约内存300 MB消耗(150个虚拟和物理150)。这些BitmapImages加入到图像对象和它们被添加到画布。问题是,当我释放所有这些图像内存不释放。我怎样才能免费存储备份
I have an application(WPF) which creates BitmapImages in huge numbers(like 25000). Seems like framework uses some internal logic so after creation there are approx 300 mb of memory consumed(150 virtual and 150 physical). These BitmapImages are added into Image object and they are added into Canvas. The problem is that when I release all those images memory isn't freed. How can I free memory back?
应用程序很简单:
的XAML
The application is simple:Xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Canvas x:Name="canvas" Grid.ColumnSpan="2"></Canvas>
<Button Content="Add" Grid.Row="1" Click="Button_Click"/>
<Button Content="Remove" Grid.Row="1" Grid.Column="1" Click="Remove_click"/>
</Grid>
代码隐藏
Code-behind
const int size = 25000;
BitmapImage[] bimages = new BitmapImage[size];
private void Button_Click(object sender, RoutedEventArgs e)
{
var paths = Directory.GetFiles(@"C:\Images", "*.jpg");
for (int i = 0; i < size; i++)
{
bimages[i] = new BitmapImage(new Uri(paths[i % paths.Length]));
var image = new Image();
image.Source = bimages[i];
canvas.Children.Add(image);
Canvas.SetLeft(image, i*10);
Canvas.SetTop(image, i * 10);
}
}
private void Remove_click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < size; i++)
{
bimages[i] = null;
}
canvas.Children.Clear();
bimages = null;
GC.Collect();
GC.Collect();
GC.Collect();
}
这是ResourceManager中的截图添加图像
This is a screenshot of ResourceManager after adding images
推荐答案
有是,我们被那里,除非你冻结他们的BitmapImage对象不被咬发布WPF中的错误。 是我们发现的问题原来的页面。它应该被固定在WPF 3.5 SP1,但我们仍然看到它在某些情况下。试着改变你这样的代码,看看如果是这样的问题:
There was a bug in Wpf that we were bitten by where BitmapImage objects are not released unless you freeze them. http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx was the original page where we discovered the issue. It should have been fixed in Wpf 3.5 sp1 but we were still seeing it in some situations. Try changing your code like this to see if that is the problem:
bimages[i] = new BitmapImage(new Uri(paths[i % paths.Length]));
bimages[i].Freeze();
我们经常冻结我们的BitmapImage对象,现在,因为我们是在WPF被监听探查看到其他实例事件上的BitmapImage和由此保持图像活着。
We routinely freeze our BitmapImage objects now as we were seeing other instances in the profiler where Wpf was listening for events on the BitmapImage and thereby keeping the image alive.
如果该Feeze()调用不是你的代码有明显的修复,我会强烈建议使用分析,如展鹏内存分析器 - 这将跟踪依赖树会告诉你它是什么,是让你的图片对象在内存中。
If the Feeze() call isn't an obvious fix for your code, I would highly recommend using a profiler such as the RedGate Memory Profiler - that will trace a dependency tree that will show you what it is that is keeping your Image objects in memory.
这篇关于垃圾收集不能回收的BitmapImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!