什么是 Java 字符串实习? [英] What is Java String interning?

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

问题描述

Java 中的字符串实习是什么,我什么时候应该使用它,以及为什么?

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() 将确保所有具有相同内容的字符串共享相同的内存.因此,如果您的姓名列表中 '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 维护在永久内存池中,与堆相比,永久内存池的大小通常有限,因此如果没有太多重复值,则不应使用 intern.

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.

更多关于使用 intern() 的内存限制

More on memory constraints of using intern()

一方面,确实可以通过以下方式删除字符串重复项将它们内化.问题是内部化的字符串去永久代,这是JVM中保留的一个区域对于非用户对象,如类、方法和其他内部 JVM对象.这个区域的大小是有限的,通常要小得多比堆.对 String 调用 intern() 有移动的效果它从堆中取出到永久代中,你冒着风险永久代空间用完.

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 中,interned 字符串不再分配在 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 字符串实习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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