JAVA-8中堆上的字符串对象数 [英] Number of String Objects on heap in JAVA-8

查看:117
本文介绍了JAVA-8中堆上的字符串对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从此堆栈溢出时的字符串对象数量,我开始知道如果我们做类似的事情:

From this Number of String Objects on stack overflow,I came to know that if we do some thing like :

String s = new String("ABC");

然后我们有两个objects,一个在堆上String,另一个在constant池上"ABC"

Then we have two objects one on heap that is String and one on constant pool that is "ABC",

但是今天我进行了堆转储,发现堆自身上有两个objects.我同样使用MAT工具,请在下面找到屏幕截图.

But today I took the heap dump and found there are two objects on heap it self. I used MAT tool for the same please find the screen shot below.

所以我的查询是,堆上是否有两个对象Char[],而对于String类的另一个,而在常量池中的一个,那么这意味着

So my query is if there are two Objects on heap one of Char[] and other for String class and one on constant pool then thus this means that

String s = new String("ABC")将总共创建3个对象.

String s = new String("ABC") will create 3 objects in total.

推荐答案

在Internet上似乎反复出现有关字符串文字和字符串池的废话.只是要强调一下,是如何定义的:

There seems to be repeated nonsense about string literals and the string pool all over the internet. Just to to emphasize, how heap in defined:

Java®虚拟机规范

2.5.3.堆

Java虚拟机具有一个 heap ,该堆在所有Java虚拟机线程之间共享.堆是运行时数据区,从中分配了所有类实例和数组的内存.

2.5.3. Heap

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

因此,无论虚拟机如何实现,所有对象都驻留在堆中,这就是定义 heap 的方式.堆是内存,所有对象实例都是从中分配的,因此,所有对象都是从堆中分配的.过去,用于字符串文字的对象和通过new创建的对象曾经生活在不同的内存区域中,但是,所有这些仍然是堆的一部分.

So, regardless of how a virtual machine implements it, all objects are living in the heap, as that’s how the term heap is defined. The heap is the memory, all object instances are allocated from, hence, all objects are allocated from the heap. In the past, objects for string literals and objects created via new used to live in different memory regions, but still, all of those were part of the heap.

在最近的JVM中,所有String实例都是在同一内存区域中创建的,无论是为文字创建的还是通过new创建的实例.

In recent JVMs, all String instances are created in the same memory region, whether created for a literal or for an instance created via new.

在任何一种情况下,用于管理字符串文字和"interned"字符串的字符串池都是这些字符串的 references 表.表本身可能位于堆外部,而对象则不在.

In either case, the string pool to manage string literals and "interned" strings is a table of references to these strings. The table itself may live outside the heap, the objects do not.

在您的示例中,您有两个String实例和一个char[]数组,因为String被实现为char[]数组的包装器,并且两个字符串共享该数组.但这是一个实现细节.在其他(较旧的)JVM中,当您使用String(String)构造函数从另一个字符串构造一个字符串时,将复制该数组.因此,在这些JVM中,您的示例将创建两个String实例和两个char[]数组实例.

In your example, you have two String instances and one char[] array, because Strings are implemented as wrappers around char[] arrays and both strings share the array. But that’s an implementation detail. In other (older) JVMs, that array got copied when you construct one string from another using the String(String) constructor. So in these JVMs, your example would create two String instances and two char[] array instances.

更加花哨的是,通过使用最新的JVM和适当的配置,JVM将识别具有不同阵列但内容相同的String实例,并将其更改为共享阵列以减少内存消耗.此功能称为 字符串重复数据删除 .关于堆栈溢出,请参见: Java 8的字符串重复数据删除功能 .

Even more fancy, with up to date JVMs and an appropriate configuration, the JVM will identify String instances with different arrays but identical content and change them to share an array to reduce the memory consumption. This feature is called String Deduplication. On Stack Overflow, see: String Deduplication feature of Java 8.

这篇关于JAVA-8中堆上的字符串对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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