我多次调用GC.Collect()后图像内存干净

我正在使用图像进行预览。 在我的工作之后,我只是将ImageSource设置为null并调用GC.Collect()但是在调用GC.Collect()之后,50%的内存仍处于保持状态,当我再次调用GC.Collect()时它仍然保持不变。

为什么我的电话要干净?

在此处输入图像描述

点击Destroy后

private void ImgDistry_Click(object sender, RoutedEventArgs e) { image.Source = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } 

在Destroy Image之后设置image.Source = null,并调用GC.Collect(); 记忆仍然暂停。 一段时间后应该自动清洁。

在此处输入图像描述

并保持内存,直到我在15-30秒后调用GC.Collect()2-3次。我可以立即清除未使用的位图内存吗?

检查video(内部代码和步骤): – https://www.dropbox.com/s/457hhjnlttbzhlp/TinyTake%20by%20MangoApps-16-08-2018-06-28-51.mp4?dl=0

 private void ImgDisplay_Click(object sender, RoutedEventArgs e) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var bitmap = new BitmapImage(); using (var stream = File.OpenRead(filePath)) { bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); } image.Source = bitmap; } private void ImgDistry_Click(object sender, RoutedEventArgs e) { image.Source = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } private void ImgSelect_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.ShowDialog(); filePath = openFileDialog1.FileName; } 

得到答案..需要在将null设置为图像源之后更新布局,然后调用GC

 private void ImgDistry_Click(object sender, RoutedEventArgs e) { image.Source = null; UpdateLayout(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); }