java - Java 的 interned 字符串会被 GC 吗?

标签 java garbage-collection

在追踪可能的内存泄漏时,我发现了以下现象。 Java 7 回收 interned Strings 或 jmap 不精确吗?

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10526 interned Strings occupying 880048 bytes.

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10514 interned Strings occupying 878984 bytes.

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10519 interned Strings occupying 879720 bytes. 

环境: Linux 版本 2.6.32-220.23.1.el6.centos.plus.x86_64 ( [email protected] ) (gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP 2004 年 6 月 19 日星期二: 2012 年英国夏令时 14:37

java版本“1.7.0_07” Java(TM) SE 运行时环境(版本 1.7.0_07-b10) Java HotSpot(TM) 64 位服务器 VM(内部版本 23.3-b01,混合模式)

最佳答案

是的,内部字符串被回收。并如 Java SE 7 Features and Enhancements 中指定。建议:

In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

关于java - Java 的 interned 字符串会被 GC 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645408/

相关文章:

java - 编译时变量的名称和生命周期

java - 在其余 Web 服务中获取下拉列表(<select> 标签)的选定值

java - 没有布局管理器的响应式 JFrame

java - 年轻代和老年代都存在字符串池?

java - 使用后将 "null"分配给每个应用程序中的对象

c# - 是否有必要在 C# 中显式删除事件处理程序

java - 冲突的依赖项 : how to choose?

Netbeans IDE 中的 Java 代码为 : Code executed is the older one!,即使我对代码进行了更改

java - 删除引用以帮助 GC 是一种好习惯吗?

android - 超过 dagger2 GC 开销限制