字符串在Java 8中使用了多少内存? [英] How much memory does a string use in Java 8?

查看:225
本文介绍了字符串在Java 8中使用了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近读了很多关于Strings内存分配的内容,如果与Java 8的内容相同,则无法找到任何细节。

I read a lot about memory allocation for Strings lately and can't find any details if things are the same with Java 8.

多少内存空间字符串如Alexandru Tanasescu在Java 8中使用?
我使用64位版本。

How much memory space would a String like "Alexandru Tanasescu" use in Java 8? I use the 64bit version.

推荐答案

Java7或更低版​​本



最小字符串内存使用量:

Java7 or lower

Minimum String memory usage :

(bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)

所以

80 = 8 * (int) ((((19) * 2) + 45) / 8)






了解字符串内存使用情况 SOURCE


To理解上面的计算,我们需要从查看String对象上的字段开始。字符串包含以下内容:

To understand the above calculation, we need to start by looking at the fields on a String object. A String contains the following:


  • 一个字符数组 - 因此是一个单独的对象 - 包含实际字符;

  • 字符串开始的数组中的整数偏移量;

  • 字符串的长度;

  • 另一个用于缓存计算的int哈希码。

  • a char array— thus a separate object— containing the actual characters;
  • an integer offset into the array at which the string starts;
  • the length of the string;
  • another int for the cached calculation of the hash code.

这意味着即使字符串不包含字符,char数组引用也需要4个字节,加上3 * 4 =三个int字段的12个字节,加上8个字节的对象头。这给出了24个字节(这是8的倍数,因此到目前为止还不需要填充字节)。

This means even if the string contains no characters, it will require 4 bytes for the char array reference, plus 3*4=12 bytes for the three int fields, plus 8 bytes of object header. This gives 24 bytes (which is a multiple of 8 so no "padding" bytes are needed so far).

然后,(空)char数组将需要另外12个字节(数组有额外的4个字节来存储它们的长度),加上在这种情况下4个字节的填充将char数组对象使用的内存增加到16的倍数。总的来说,空字符串使用40个字节。

Then, the (empty) char array will require a further 12 bytes (arrays have an extra 4 bytes to store their length), plus in this case 4 bytes of padding to bring the memory used by the char array object up to a multiple of 16. So in total, an empty string uses 40 bytes.

如果 String 包含19个字符,那么String对象本身仍然需要24个字节。但是现在char数组需要12个字节的标头加上19 * 2 = 38个字节用于17个字符。由于12 + 38 = 50不是8的倍数,我们还需要向上舍入到8(56)的下一个倍数。总的来说,我们的19个字符字符串将耗尽56 + 24 = 80个字节。

If the String contains, say, 19 characters, then the String object itself still requires 24 bytes. But now the char array requires 12 bytes of header plus 19*2=38 bytes for the seventeen chars. Since 12+38=50 isn't a multiple of 8, we also need to round up to the next multiple of 8 (56). So overall, our 19-character String will use up 56+24 = 80 bytes.


Java 8没有偏移量长度了。只有哈希 CharArray
@ Thomas Jungblut




  • 一个char数组 - 因此a包含实际字符的单独对象;

  • 字符串开始的数组中的整数偏移量;

  • < strike>字符串的长度;

  • 另一个用于缓存计算哈希码的int。

    • a char array— thus a separate object— containing the actual characters;
    • an integer offset into the array at which the string starts;
    • the length of the string;
    • another int for the cached calculation of the hash code.
    • 因此,在Java8中,为字符串计算内存的方法保持不变,但由于缺少偏移量和<$ c $,您必须减少8个字节。 c>长度。

      So, in Java8 the way to calculate memory for strings remains same but you must subtract 8 bytes less due to the missing offset and length.

      这篇关于字符串在Java 8中使用了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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