将'var'对象重新分配给新对象类型是否会使旧对象从内存中删除? [英] Does reassignment of 'var' object to a new object type make the old object delete from memory?

查看:81
本文介绍了将'var'对象重新分配给新对象类型是否会使旧对象从内存中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用'var'对象用这样的新Page对象更新WPF Frame Control,





 Frame frame =  new  Frame(); 

var page = new Page1();

frame.Content = page;

var page = new Page2();

frame.Content = page;

var page = new Page3();

frame.Content = page;





现在,Page1和Page2对象是否会保留在Memory中?或者将被垃圾收集器消耗?因为我观察到应用程序的内存消耗正在缓慢增加,但找不到合适的模式。

解决方案

嗯......不,他们赢了T。但另一方面,它们也不会立即消失。如果他们这样做,内存使用量可能不会缩小,特别是如果它们足够大以适应从未压缩的大对象堆。



首先要做的事情是请注意,当新的内存请求出现问题并且需要执行某些操作时,垃圾收集器才会介入 - 它不会一直运行。所以很有可能让垃圾收集器在你的应用程序生命周期中永远不会运行。

第二个是大型对象在自己的堆上运行,从不压缩,只是重用。因此,如果你的页面项目很大(或包含大对象),它们可能不会很好地反映在内存使用中。



看看这里: http://msdn.microsoft.com/en-us/magazine/cc534993.aspx [ ^ ]讨论监视堆和内存使用情况。

I've been using 'var' object to update WPF Frame Control with new Page objects like this,


Frame frame = new Frame(); 

var page = new Page1();

frame.Content = page;

var page = new Page2();

frame.Content = page;

var page = new Page3();

frame.Content = page;



Now, will the Page1 and Page2 objects remain in Memory? Or will be consumed by garbage collector? Because I've observed that the Memory Consumption of the application is slowly increasing, but couldn't find a decent pattern.

解决方案

Well...no, they won't. But on the other hand, they won't vanish immediately either. And if they do, the memory usage may not shrink, particularly if they are big enough to fit on the Large Object Heap which is never compacted.

The first thing to notice is that the garbage collector only gets involved when a new memory request has a problem and something needs to be done - it doesn't run all the time. So it is quite possible to have the garbage collector never run during you application lifecycle.
The second is that large objects go on their own heap, which is never compacted, just reused. So if your Page items are big (or contain big objects), they may not be reflected in the memory usage very well.

Have a look here: http://msdn.microsoft.com/en-us/magazine/cc534993.aspx[^] which talks about monitoring the heaps and memory usage.


这篇关于将'var'对象重新分配给新对象类型是否会使旧对象从内存中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆