Image memory clean after i call GC.Collect() many time

旧巷老猫 提交于 2019-12-06 10:05:01

问题


I am using the image to preview. after my work, I just set ImageSource to null and call GC.Collect() but after call GC.Collect() 50% memory is still on hold and its hold up to when I call again GC.Collect() ..

why it takes my call to clean?.

After Click Destroy

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

After Destroy Image set image.Source = null, and call GC.Collect(); memory still on hold. it should be auto clean after some time.

and it holds memory until I call GC.Collect() 2-3 times after 15-30 seconds..how can i immediately clean unused bitmap memory?

Check Video(code and steps inside):- 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;
    }

回答1:


Got the answer.. need to update layout after set null to image source then call 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();
    }


来源:https://stackoverflow.com/questions/51877794/image-memory-clean-after-i-call-gc-collect-many-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!