什么是Java String interning? [英] What is Java String interning?

查看:168
本文介绍了什么是Java String interning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Java中的 String Interning ,什么时候我应该使用它?为什么?

What is String Interning in Java, when I should use it, and why?

推荐答案

http:// docs。 oracle.com/javase/7/docs/api/java/lang/String.html#intern()

基本上做String.intern()on一系列字符串将确保具有相同内容的所有字符串共享相同的内存。因此,如果你有'john'出现1000次的名字列表,你可以通过实习确保只有一个'john'实际分配了内存。

Basically doing String.intern() on a series of strings will ensure that all strings having same contents share same memory. So if you have list of names where 'john' appears 1000 times, by interning you ensure only one 'john' is actually allocated memory.

这对于减少程序的内存要求。但请注意,缓存由JVM在永久内存池中维护,与堆相比,其大小通常有限,因此如果没有太多重复值,则不应使用实习。

This can be useful to reduce memory requirements of your program. But be aware that the cache is maintained by JVM in permanent memory pool which is usually limited in size compared to heap so you should not use intern if you don't have too many duplicate values.

更多关于使用实习生的内存限制()

More on memory constraints of using intern()


一方面,确实可以通过
内部化它们来删除String重复项。问题是内化字符串转到永久生成
,这是JVM的一个区域,为非用户对象保留
,如类,方法和其他内部JVM
对象。这个区域的大小是有限的,通常比堆大得多
。在字符串上调用intern()会将
从堆中移出到永久代中,并且你冒着
用完PermGen空间的风险。

On one hand, it is true that you can remove String duplicates by internalizing them. The problem is that the internalized strings go to the Permanent Generation, which is an area of the JVM that is reserved for non-user objects, like Classes, Methods and other internal JVM objects. The size of this area is limited, and is usually much smaller than the heap. Calling intern() on a String has the effect of moving it out from the heap into the permanent generation, and you risk running out of PermGen space.

-
来自: http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html

从JDK 7(我的意思是在HotSpot中),有些东西发生了变化。

From JDK 7 (I mean in HotSpot), something has changed.


在JDK 7中,不再分配实习字符串在Java堆的永久生成中,而是分配在Java堆的主要部分(称为年轻和老一代),以及应用程序创建的其他对象。此更改将导致更多数据驻留在主Java堆中,并且永久生成中的数据更少,因此可能需要调整堆大小。由于此更改,大多数应用程序只会看到堆使用中的相对较小的差异,但是加载许多类或大量使用String.intern()方法的较大应用程序将看到更显着的差异。

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 SE 7特性和增强功能

更新:实现的字符串从Java 7开始存储在主堆中。 http://www.oracle.com/technetwork/java /javase/jdk7-relnotes-418459.html#jdk7changes

Update: Interned strings are stored in main heap from Java 7 onwards. http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html#jdk7changes

这篇关于什么是Java String interning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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