垃圾回收负责所有内存管理 [英] Garbage collectotion takes care of all the memory management

查看:69
本文介绍了垃圾回收负责所有内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的印象是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子句混用),请参见 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 implement System.IDisposable. You need to make sure IDisposable.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 is System.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屋!

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