当我按下GoBack时,内存不会下降

本文关键字:内存 GoBack | 更新日期: 2023-09-27 18:10:28

当GoBack调用函数Dispose()时

 private void Dispose()
    {

        ImageBrush brushRoot = LayoutRoot.Background as ImageBrush;
        if (brushRoot != null)
        {
            ((BitmapImage)brushRoot.ImageSource).UriSource = null;
            brushRoot.ImageSource = null;
            LayoutRoot.Background = null;
        }
        gridHeader_hotNews.Children.Clear();
        gridHeader_hotNews = null;
        if (grid_Two.Background as ImageBrush != null)
        {
            ((BitmapImage)(grid_Two.Background as ImageBrush).ImageSource).UriSource = null;
            (grid_Two.Background as ImageBrush).ImageSource = null;
            grid_Two.Background = null;
        }
        if (btn_Two.Background as ImageBrush != null)
        {
            ((BitmapImage)(btn_Two.Background as ImageBrush).ImageSource).UriSource = null;
            (btn_Two.Background as ImageBrush).ImageSource = null;
            btn_Two.Click -= new RoutedEventHandler(btn_Two_Click);
            btn_Two.Background = null;
        }
        btn_Two = null;
        grid_Two.Children.Clear();
        grid_Two = null;
        grid_Content.Children.Clear();
        grid_Content = null;
        if (grid_footer.Background as ImageBrush != null)
        {
            ((BitmapImage)(grid_footer.Background as ImageBrush).ImageSource).UriSource = null;
            (grid_footer.Background as ImageBrush).ImageSource = null;
            grid_footer.Background = null;
        }
        if (btnspeaker.Background as ImageBrush != null)
        {
            ((BitmapImage)(btnspeaker.Background as ImageBrush).ImageSource).UriSource = null;
            (btnspeaker.Background as ImageBrush).ImageSource = null;
            btnspeaker.Background = null;
        }
        btnspeaker = null;
        grid_footer.Children.Clear();
        grid_footer = null;
        LayoutRoot.Children.Clear();
        LayoutRoot = null;
        GC.SuppressFinalize(this);
        GC.WaitForPendingFinalizers();
    }

,但仍然使用3到5 MB。当我调用GoBack事件时,我如何恢复原始内存?

请帮助我,我希望内存释放时回I页或我处置的对象。

当我按下GoBack时,内存不会下降

垃圾收集器只在需要运行时运行,因此它不一定会在您运行Dispose时立即运行。

你可以通过调用GC. collect()来强制GC进行回收;但这可能会对应用程序的性能产生影响。这是因为当GC运行时,它会停止应用程序中所有正在执行的线程,并检查堆中的每个对象,以查看它是否仍被引用或正在使用。虽然垃圾回收器很快,但重复调用GC会有开销。

正如其他人评论的那样,最好让GC自己管理自己。