如何在Java中销毁String池中的引用? [英] How can I destroy reference from String pool in Java?

查看:228
本文介绍了如何在Java中销毁String池中的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串是不可变的。当我宣布:

Strings are immutable. When I declare:

String a = "abc";
String a1 = "abc";

两个对象都指向相同的位置。那么如何从String池中销毁这个abc引用?

Both objects refer to same location. So how can I destroy this "abc" reference from String pool?

我的用例是我正在开发一个内存较少的硬件应用程序,为此,我需要清除String池中的引用以节省内存。

My use case is that I am developing a hardware application with less memory and for this, I need to clear the references from String pool to save memory.

推荐答案

不,通常你不能破坏Java中字符串池的引用手动。

No, typically you can not "destroy reference from String pool in Java" manually.

我认为你瞄准它的主要原因是为了避免内存不足错误。在Java 6天内,所有实习字符串都存储在PermGen中 - 堆的固定大小部分主要用于存储加载的类和字符串池。除了显式实现的字符串外,PermGen字符串池还包含程序中先前使用的所有文字字符串。 Java 6中字符串池的最大问题是它的位置--PermGen。 PermGen 具有固定大小,无法在运行时展开。您可以使用 -XX:MaxPermSize = N 选项进行设置。这将导致内存泄漏和内存不足错误。

The main reason I suppose why you are targeting it is to avoid out of memory errors. In Java 6 days all interned strings were stored in the PermGen – the fixed size part of heap mainly used for storing loaded classes and string pool. Besides explicitly interned strings, PermGen string pool also contained all literal strings earlier used in your program. The biggest issue with string pool in Java 6 was its location – the PermGen. PermGen has a fixed size and can not be expanded at runtime. You can set it using -XX:MaxPermSize=N option. This would lead to memory leaks and out of memory errors.

在Java 7中 - 字符串池重新定位到。这意味着您不再受限于单独的固定大小的内存区域。所有字符串现在都位于堆中,与大多数其他普通对象一样。

In Java 7 – the string pool was relocated to the heap. It means that you are no longer limited by a separate fixed size memory area. All strings are now located in the heap, as most of other ordinary objects.

您还可以通过配置 -XX来增加字符串池大小:StringTableSize = N 即可。
如果您不确定字符串池的使用情况,请尝试 - XX:+ PrintStringTableStatistics JVM参数。它将在程序终止时打印字符串池使用情况。

You may also increase the string pool size by configuring -XX:StringTableSize=N. If you are not sure about the string pool usage, try -XX:+PrintStringTableStatistics JVM argument. It will print you the string pool usage when your program terminates.

在JDK中,还有一个名为jmap的工具,可用于找出你的申请中的实习字符串数量。

In JDK, there is also a tool named jmap which can be used to find out number of interned strings in your application.

jmap -heap process_id

例如:

jmap -heap 18974

除了其他输出外,此命令还输出多个实习字符串及其占用的空间 xxxxxx interned字符串占用xxxxxx字节

Along with other output, this command also outputs number of interned strings and the space they occupy "xxxxxx interned Strings occupying xxxxxx bytes."

字符串池中对象的垃圾收集的规则与其他字符串相同或任何其他对象。但事实上,对应于String文字的String对象大多数总是可以访问的,因为在使用该文字的每个方法的代码中都存在对字符串对象的隐式引用,因此通常它们不是垃圾收集的候选者。
但是,情况并非总是如此。如果文字是在动态加载的类中定义的(例如,使用Class.forName(...)),则可以安排卸载类。如果发生这种情况,那么文字的String对象将无法访问,并且当包含实习字符串的堆得到GC时将被回收。

The rules for garbage collection of objects in the String pool are the same as for other Strings or any other object. But the fact that the String objects that correspond to String literals mostly are always reachable since there is an implicit reference to the string object in the code of every method that uses the literal and so typically they are not candidates for garbage collection. However, this is not always the case. If the literal was defined in a class that was dynamically loaded (e.g. using Class.forName(...)), then it is possible to arrange that the class is unloaded. If that happens, then the String object for the literal will be unreachable, and will be reclaimed when the heap containing the interned String gets GC'ed.

这篇关于如何在Java中销毁String池中的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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