垃圾回收负责所有内存管理 [英] Garbage collectotion takes care of all the memory management
本文介绍了垃圾回收负责所有内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的印象是GC将不时处理所有参考,以及内存管理.
该对象可以是文件流或excel对象等.
我们完全不必担心内存管理.前天,一位朋友说不,当我们明确创建流对象时,我们必须刷新.
请清除疑问
在此先感谢
Hi All,
I am under impression that GC will take care of all the references , memory management time to time.
The object may be of file stream or excel object etc.
We need not worry about the memory management at all. and day before yesterday one of friend said no we have to flush when we create stream objects explicitly.
Please clear the doubts
Thanks in advance
推荐答案
选中此链接,可能会对您有所帮助
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx [ ^ ]
Check this link, this may be helpfull
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx[^]
是的,我们不必担心内存管理.但是,刷新流与内存管理无关.我们需要注意流的刷新和关闭.注意:许多类都实现了System.IDisposable
.您需要确保在完成此类工作时始终调用此类的IDisposable.Dispose
.这样做的一种好方法是使用"using"语句(不要与using子句混用),请参见 ^ ].
在某些类中,处理失败可能会导致其他麻烦:非托管内存泄漏.
最后,如果您认为GC将您从托管内存泄漏中救了出来,请三思.是的,它完全可以防止意外泄漏,当您忘记删除某些对象(没有删除)时,但是您很容易因设计错误而导致泄漏.请记住,当运行的代码松开对该对象的所有引用时,该对象将安排执行.即使对象A引用了对象B,B引用了C,C引用了A,这种情况也得以解决,并且如果不再有对它们的引用,则它们都将被销毁.
想象一下,您创建和删除了一些对象(例如Forms),并期望当这些对象的数量为零时,内存分配会进入同一点.会工作的.现在,假设您有一个全局对象(例如Dictionary),该对象用于容纳所有这些对象,以便按名称快速查找它们.如果您忘记从Dictionary实例中删除引用,则引用将永远被收集在那里,从而有效地防止了销毁-您造成了内存泄漏.
只要您在Application Domain中有一些 singleton ,它就可能发生.解决此问题的另一种方法是System.WeakReference
,请参见 http://msdn.microsoft.com /en-us/library/system.weakreference.aspx [ ^ ].
另请参阅我最近对相关主题的回答:
在循环内部延迟变量会导致内存泄漏吗? [ ^ ].
—SA
Yes, we don''t have to worry about memory management. However, flush of the stream has nothing to do with memory management. We need to take care about the flush and closing of the stream. Pay attention: many classes implementSystem.IDisposable
. You need to make sureIDisposable.Dispose
for such classes is always called when their work is done. One good way of doing so is using "using" statement (not to be mixed up with using clause), see http://msdn.microsoft.com/en-us/library/yh598w02%28v=VS.100%29.aspx[^].
In some classes a failure to dispose can cause additional trouble: a leak of unmanaged memory.
Finally, if you think that GC saves you from leak of managed memory, think again. Yes, it totally guards you against accidental leaks, when you forget to delete some object (there is no delete), but you can easily cause the leak by design mistakes. Remember, an object is scheduled for execution when the running code looses all references to it. Even if object A references object B, B references C and C references A this situation is resolved and they all are destroyed if there are no more references to either of them.
Imagine you create and delete some objects (for example, Forms) and expect the memory allocation to come into the same point when the number of these objects comes to zero. It will work. Now imagine you have a global object such as Dictionary used to hold all those objects for the purpose of quick finding them, say, by name. If you forget to delete references from an instance of the Dictionary, references will be collected there forever effectively preventing destruction — you created a memory leak.
It can happen whenever you have some singleton in the Application Domain. One other technique to solve this problem isSystem.WeakReference
, see http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].
Please see also my recent answer on a related topic:
deferring varirable inside the loop can cuase memory leak?[^].
—SA
这篇关于垃圾回收负责所有内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文