垃圾收集器和静态类,变量 [英] Garbage collector and static class, variable

查看:109
本文介绍了垃圾收集器和静态类,变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一点是在我的脑海,因为最后几天琢磨。
我想知道的垃圾收集器的静态类是如何工作的,变量?

One point is wondering in my mind since last few days. I want to know how Garbage collector work with static classes, variables?

我们都知道垃圾回收器保持已经在应用程序中创建对象的跟踪和自动删除他们时,他们都不再使用。
对于静态类没有创建对象,并将其与应用程序调试加载到内存中。

As we all know Garbage collector keep track of objects that has been created in application and removed them automatically when they are no longer in use. For static class no object is created and it loaded in the memory with the application debug.

所以,垃圾收集处理静态类?

So Garbage collector handle static classes?

推荐答案

静态类不需要,因为它们不是对象被删除,他们不消耗内存(除加载实际类代码等在.NET基础设施)。在静态类另一方面静态字段是消耗内存的对象。因为它们是用于应用程序的完整生命周期可以访问他们不会被收集。如果你想释放内存,你应该设置字段设置为null,使该领域一直指向的对象就符合GC

Static classes don't need to be removed since they are not objects and they don't consume memory (other than the actual class code being loaded by the .NET infrastructure). On the other hand static fields in static classes are objects that consume memory. They won't be collected since they are accessible for the full lifetime of the application. If you want to free the memory you should set the field to null so that the object the field has been pointing to becomes eligible for GC

这篇关于垃圾收集器和静态类,变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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