当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 Page或我处理对象时释放内存。
答案 0 :(得分:2)
垃圾收集器仅在需要运行时运行,因此一旦运行Dispose,它就不会一直运行。
您可以通过调用GC.Collect()强制收集GC。但这可能会对您的应用程序性能产生影响。这是因为当GC运行时,它会停止应用程序中的所有正在执行的线程,并检查堆中的每个对象以查看它是否仍被引用或正在使用中。虽然垃圾收集器速度很快,但重复调用GC会产生开销。
正如其他人评论所说的那样,让GC管理自己是最好的。