GC(垃圾收集器)无法收集值类型对象吗? [英] Does value type object cannot be collected by GC (Garbage collector)?

查看:73
本文介绍了GC(垃圾收集器)无法收集值类型对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一篇文章,其中一个类有三个变量声明。



类型INT和1类型字符串(所有私有变量)



在那篇文章中写了一个控制传递到达方法的结尾,两个int类型的变量范围将以aromatically结束但是对于字符串类型变量GC将检查它是没有被任何程序实例引用然后GC会收集它吗?



这是否意味着如果是,那么GC将不会处理值类型对象?

I was reading an article in which one class has three variable declaration.

2 of type INT and 1 of type string in a class (all private variables)

In that article it was written one the control pass reach to the end of method both int type variable scope will end aromatically but for string type variable GC will check it is not referred by any instance of program then GC will collect it?

Does this mean GC won't handle value type object if yes then how these are managed?

推荐答案

我很困惑:你描述了一个带有三个变量的类;一个是字符串。然后你谈谈一个方法。变量的范围将取决于它们的创建位置:在类的范围内,或在类的范围内的方法中。



字符串是一种特殊情况:您通过在设计时/代码时或在运行时定义创建的每一个都是 interned :放入一个名为intern pool的特殊CLR表中。这样做的原因是使所有具有相同内容的字符串只有一个参考点,因此查找和比较可以非常快。



实习字符串成为大会本身的一部分,并不是垃圾收集。



请参阅:[ ^ ],[ ^ ]。



一般来说,这将是一个特殊的用例,程序员需要直接操作实习池中的内容,或者应用程序中给定字符串的实习状态。



在大多数情况下,创建太多字符串的程序(例如,通过连接)可以重新计算使用'StringBuilder对象。
I'm confused: you describe a Class with three variables; one is a string. Then you talk about a Method. The variables' scope will depend on where they are created: in the Class' scope, or in a method in the Class' scope.

Strings are a special case: every one you create through defining at design-time/code-time, or at run-time, is interned: put in a special CLR table called the "intern pool." The reason for that is to make all strings with the same content have only one reference point, so look-up, and comparison, can be very fast.

Interned strings become part of the Assembly itself, and are not garbage collected.

See: [^], [^].

In general it would be an exceptional use case that would need for the programmer to directly manipulate what is in the intern pool, or the interned status of a given string in your application.

In most cases programs that create too many strings (by concatenation, for example) can be re-factored to use the 'StringBuilder object.


非引用erg,不能从多个地方引用类型o在取消分配内存之前,GC无需检查更多参考资料。





Non-reference types can't be referenced from multiple places, ergo the GC needn't check for more references before deallocating the memory.


Sudhir Dutt Rawat写道:
Sudhir Dutt Rawat wrote:

范围将以aromatically结束

scope will end aromatically



:笑::笑::笑::笑:


:laugh: :laugh: :laugh: :laugh:


这篇关于GC(垃圾收集器)无法收集值类型对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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