StringPool中的String对象寿命 [英] String object life in StringPool

查看:86
本文介绍了StringPool中的String对象寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道JVMString变量存储在单独的StringPool中. 每当我们创建String (eg. String s1 = "anystring")时,它都会存储为常量&当我们创建新的String变量作为相同的值时,除了创建新的String常量之外,它使用相同的引用.

All we know that JVM stores String variables in a separate StringPool. Every time when we create a String (eg. String s1 = "anystring") it stores as a constant & when we create new String variable as same value it uses same reference except creating new String Constant.

我只想知道StringPool中该变量的寿命是多少.是application level还是JAVA environment level.

I just want to know that what is the life of that variable in StringPool. is it application level or JAVA environment level.

问题:如果是JVM级,那么不同的应用程序是否使用相同的引用?

Question: If JVM level, then does different application uses same references?

我不确定我在String理论上所处的位置.但是我正在寻找答案.

I am not sure where I am up to in String theory. But I'm looking for answer.

推荐答案

字符串池中对象的生存期受与任何其他对象相同的规则支配.只要对象是 reachable ,它就会继续存在. 不可达一段时间后,它将被回收.

The lifetime of an object in the string pool is governed by the same rules as any other object. An object will continue to exist as long as it is reachable. Some time after it becomes unreachable it will be reclaimed.

关于与类中的文字相对应的String对象的唯一区别是,可以通过类的代码访问该对象.通常,类代码的生存期就是JVM的生存期.但是,如果该类是动态加载的,然后继续使加载它的类加载器无法访问,等等,则该类可能无法访问,因此文字对象可能变得无法访问.

The only thing that is "different" about a String object that corresponds to literal in a class is that the object is reachable via the code for the class. Normally, the lifetime of the code of a class is the lifetime of the JVM. However, if the class was dynamically loaded, and you then proceed to make the classloader that loaded it unreachable, etcetera, then the class may be come unreachable, and hence the literal objects may become unreachable.

我只想知道StringPool中该变量的寿命是多少.是应用程序级别还是JAVA环境级别.

I just want to know that what is the life of that variable in StringPool. is it application level or JAVA environment level.

应用程序级别"和"Java环境级别"的含义不清楚,但是在正常情况下,正在运行的应用程序的生存期与JVM的生存期相同.但是,无论哪种方式,都取决于可达性分析.即GC用于确定是否仍可以在计算中使用对象的跟踪"过程.

It is not clear what you mean by "application level" and "Java environment level", but under normal circumstances the lifetime of the running application is the same as the lifetime of the JVM. But either way, it is all determined by reachability analysis; i.e the "tracing" process that the GC uses to determine if an object can still be used in the computation.

实际上,您的问题中的这一说法可以说是不正确的.

Actually, this statement in your Question is arguably incorrect.

我们都知道JVM将String变量存储在单独的StringPool中.

All we know that JVM stores String variables in a separate StringPool.

实际上,在Java 7中,字符串池实际上存储在常规堆中,而不是permgen堆中. (此外,您实际上是说String对象不是String变量.对象和变量根本不是同一件事……)

In fact, in Java 7 the string pool is actually stored in the regular heap, not the permgen heap. (And besides, you really means String objects not String variables. Objects and variables are not the same thing at all ...)

这篇关于StringPool中的String对象寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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