字符串池-字符串是否始终存在于常量池中? [英] String pool - do String always exist in constant pool?

查看:88
本文介绍了字符串池-字符串是否始终存在于常量池中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用文字创建字符串时,它将存储在池中.但是,当使用new运算符创建String对象时,它将对象存储在Heap中.

When string is created by using literal, it gets stored in pool. But when new operator is used to create String object, it stores the object in Heap.

但是堆中的对象只是指向存储在池中的文字的指针,还是存储在堆中且符合GC条件的简单String对象?

But is the object in heap just a pointer to literal stored in pool or is it a simple String object stored in heap which is eligible for GC?

推荐答案

术语:

  • 常量池是(每个).class文件中的一个区域,其中包含各种常量,包括字符串.常量池中没有 runtime 对象.它是文件的区域.

字符串池是JVM用于管理某些类型的字符串的运行时数据结构.(特别是,与文字对应的 String 对象,以及通过 String :: intern()添加到池中的 String 对象.)

The string pool is a runtime data structure used by the JVM to manage certain kinds of strings. (Specifically, String objects that correspond to literals, and String objects added to the pool by String::intern().)

您的问题实际上是在谈论字符串池,而不是常量池.

Your question is actually talking about the string pool, not the constant pool.

要回答您的问题:

字符串池-字符串是否始终存在于常量池中吗?

String pool - do String always exist in constant pool?

不.使用 new String()创建的字符串对象在字符串池或常量池中都不存在.

No. A string object created using new String() doesn't exist in either the string pool or the constant pool.

使用文字创建字符串时,它将存储在池中.

When string is created by using literal, it gets stored in pool.

它(已经!)存在常量池,并在字符串池中创建.(实际的创建可以在类加载时,也可以在首次使用文字时进行.这取决于Java的实现.)

It (already!) exists the constant pool and gets created in the string pool. (The actual creation can be at class load time, or when the literal is first used. This depends on the Java implementation.)

但是当使用new运算符创建String对象时,它将对象存储在Heap中.

But when new operator is used to create String object, it stores the object in Heap.

是的.但是字符串池也是Heap的一部分.就像我说的那样,它是一种数据结构,而不是存储区域.

Yes. But the string pool is also part of the Heap. Like I said, it is a data structure, not a region of storage.

(过去,字符串池生活在一个称为PermGen堆的特殊堆中.但是PermGen被其他东西(MetaSpace)所替代,并且字符串池不再使用任何一个…….

(In the old days, the string pool lived in a special heap called the PermGen heap. But PermGen was replaced with something else (MetaSpace), and the string pool doesn't use either ... anymore.

但是堆中的对象只是指向存储在池中的文字的指针,还是存储在堆中且符合GC条件的简单String对象?

But is the object in heap just a pointer to literal stored in pool or is it a simple String object stored in heap which is eligible for GC?

这真的很困惑.

所有字符串都表示为(a)堆中的 String 对象.甚至字符串池中的字符串.即使字符串池位于PermGen中.

All strings are represented as String objects in the (a) heap. Even strings in the string pool. Even when the string pool was in PermGen.

所有无法访问的 String 对象都可以进行垃圾回收.即使对于字符串池中的字符串.即使对于表示字符串文字的 String 对象.

All String objects that are unreachable are eligible for garbage collection. Even for strings in the string pool. Even for String objects that represent string literals.

但是...等等...那么字符串文字可以被垃圾回收吗?

But ... wait ... so can string literals be garbage collected?

是的!如果表示字符串文字的 String 对象在运行时变得不可访问,则可以像其他任何 String 对象一样进行垃圾回收.

Yes!! If a String object that represents a string literal becomes unreachable at runtime it is eligible for garbage collection, just like any other String object.

如果使用字符串的代码对象变得不可达,则字符串字符串可能变得不可达.当类加载器变得不可访问时,可能会发生这种情况.

A string literal can become unreachable if the code object(s) that use the literal become unreachable. It can happen, when a classloader becomes unreachable.

是的,收集了PermGen 垃圾.至少从JDK 1.2起.(IIRC Java 1.0甚至1.1都没有为PermGen堆实现GC.但这在很久以前就已解决.)

And yes, PermGen was garbage collected. At least since JDK 1.2. (IIRC Java 1.0 and maybe 1.1 didn't implement GC for the PermGen heap. But that was fixed a long time ago.)

这篇关于字符串池-字符串是否始终存在于常量池中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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