When does garbage collection get triggered in C#?(垃圾收集何时在 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.Collectmethod 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/en-us/library/ee787088%28v=vs.110%29.aspx#conditions_for_a_garbage_collection
这篇关于垃圾收集何时在 C# 中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:垃圾收集何时在 C# 中触发?
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
