什么时候在C#中触发垃圾回收? [英] When does garbage collection get triggered in C#?

查看:498
本文介绍了什么时候在C#中触发垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多有关垃圾回收的内容,例如垃圾的产生,范围等,但想知道何时触发垃圾回收?

I read many things about garbage collection like it's generation, scope etc but want to know when does the garbage collection gets triggered ? an example will be really helpful if possible.

谢谢,

推荐答案

以下条件之一为真时,将进行垃圾回收:

Garbage collection occurs when one of the following conditions is true:


  • 系统的物理内存不足。

  • 托管堆上分配的对象使用的内存超过了可接受的阈值。此阈值会在过程运行时不断调整。

  • 调用 GC.Collect 方法。在几乎所有情况下,您不必调用此方法,因为垃圾收集器会连续运行。此方法主要用于特殊情况和测试。

  • The system has low physical memory.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

来源: https://msdn.microsoft.com/zh-CN/library/ee787088%28v=vs.110 %29.aspx#conditions_for_a_garbage_collection

这篇关于什么时候在C#中触发垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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