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

问题描述

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

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

推荐答案

基本上做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()

-
来自:

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

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

- 来自

更新:实现的字符串从Java 7开始存储在主堆中。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:19