Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我按下GoBack时,内存不会下降_C#_Windows Phone 7_Windows Phone 7.1 - Fatal编程技术网

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

C# 当我按下GoBack时,内存不会下降,c#,windows-phone-7,windows-phone-7.1,C#,Windows Phone 7,Windows Phone 7.1,当我在中调用函数Dispose()时 但它仍然使用3到5 MB。调用GoBack事件时如何恢复原始内存 请帮助我,我希望在返回页面或处理对象时释放内存。垃圾收集器仅在需要运行时运行,因此它不一定在您运行Dispose时立即运行 可以通过调用GC.collect()强制GC进行收集;但这可能会对你的应用程序性能产生影响。这是因为当GC运行时,它会停止应用程序中所有正在执行的线程,并检查堆中的每个对象,以查看它是否仍然被引用或正在使用。虽然垃圾收集器速度很快,但重复调用GC会带来开销 正如其他评论人

当我在中调用函数Dispose()时

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


请帮助我,我希望在返回页面或处理对象时释放内存。

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

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


正如其他评论人士所说,最好让GC自己管理。

此代码不会用GC清除所有对象,因为您仍然持有大量引用。但是正常情况下这应该没有问题-GC会很好地完成它的工作-我可以问你为什么需要这样做吗?@thong调用
GC
不会强制它开始directly@Carsten科尼格!谢谢回答问题,我想在返回页面时回复内存,但内存减少很少。如何管理内存,我释放对象引用,数组。。。。但是仍然没有解决?乍一看,你在那里做的唯一对垃圾收集有帮助的事情就是取消订阅Click事件。您可以删除所有其余的。
 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();
    }