垃圾收集的用途是什么?如何在代码中执行? [英] What is the use of garbage collection ?How to do it in code?

查看:61
本文介绍了垃圾收集的用途是什么?如何在代码中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net中的垃圾收集是什么?

有什么用?

我们应该在所有代码中使用它吗?

如何在代码中执行此操作?

请给我一些代码现实生活示例?

解决方案

通过了解使用垃圾收集它存在并且永远不会触及它。为什么?首先,了解它是如何工作的: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) [ ^ ]。







此外,这种理解应该让您了解托管代码中的内存泄漏仍然是可能的,但是几乎没有随意的,主要是设计。为了避免它们,你需要的只是再次理解 - 理解如何不做一些真正愚蠢的事情。请参阅我在相关问题上的详细解答:

记忆MDI表格中的管理 [ ^ ],

摆脱导致内存不足的公共静态列表的最佳方法 [ ^ ],

WPF DataBinding中的内存泄漏 [ ^ ],

推迟循环中的变量会导致内存泄漏? [ ^ ],

Garbage collectotion负责所有内存管理 [ ^ ]。



-SA

有关垃圾收集器的更多信息,请参阅 .NET中的垃圾收集 [ ^ 和互联网上提供的许多其他页面。



您无法通过托管代码对GC做很多事情。

检查将对象标记为准备删除的dispose方法 - Dispose方法 [ ^ ]。


What is garbage collection in .net ?
What is the use of that?
Should we use it in all our code\projects?
How to do it in code?
Please get me some code real life example ?

解决方案

Use garbage collection by knowing that it exists and by never touching it. Why? To start with, understand how it works: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[^].

[EDIT]

Also, this understanding should bring you to the understanding that memory leaks in managed code are still possible, but hardly random ones, mostly by design. To avoid them, all you need is, again understanding — understanding of how not to do something really stupid. Please see my detailed answers on related issues:
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Memory leak in WPF DataBinding[^],
deferring varirable inside the loop can cuase memory leak?[^],
Garbage collectotion takes care of all the memory management[^].

—SA


To more about the Garbage Collector, go through Garbage Collection in .NET[^] and many other pages available on the internet.

You cannot do much with GC through managed code.
Check the dispose method for marking objects as ready to delete - Dispose method[^].


这篇关于垃圾收集的用途是什么?如何在代码中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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