.net垃圾收集器什么时候运行? [英] When does the .net garbage collector run?

查看:142
本文介绍了.net垃圾收集器什么时候运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调试.net代码时,如何知道垃圾收集器何时运行?

When I debug .net code, how can I know when the garbage collector runs?

我不想控制垃圾回收器何时运行。我只希望能够知道它什么时候运行。我有一些资源不足的代码。我知道资源没有被使用;我想知道GC何时运行以释放它们。

I do not wish to control when the garbage collector runs. I only wish to be able to know when it is running. I have some code that is running out of resources. I know that the resources are not being used; I want to know when the GC is running to free them.

呵呵,我想到的资源是来自Sql连接池的连接,而不是内存:-)

Oh, and the resources I have in mind are connections from a Sql connection pool, not memory :-)

推荐答案

您必须使用.NET 4.0,您所要求的内容在耳机版本中不受支持。

You have to use .NET 4.0, what you are asking for isn't supported in eariler versions.

本质上,您在循环中调用WaitForFullGCApproach和WaitForFullGCComplete方法。 WaitForFullGCApproach将阻止,直到您看到GC,WaitForFullGCComplete将阻止,直到GC完成。

Essentially you call the WaitForFullGCApproach and the WaitForFullGCComplete methods in a loop. WaitForFullGCApproach will block until you see a GC, WaitForFullGCComplete will block until the GC is complete.

请仔细阅读本文。如果您使用此方法,那么您负责确定垃圾收集实际发生。如果你搞砸了,你可以打破GC,并快速耗尽内存。

Please read this article carefully. If you use this method then you are responsible for makign sure the garbage collection actually occurs. If you mess this up you could break the GC and quickly run out of memory.

http://msdn.microsoft.com/en-us/library/cc713687.aspx

这篇关于.net垃圾收集器什么时候运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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