java - s 在 Java 中占用 3 个字节吗?当: String s = "abc"; Java

标签 java memory compiler-construction jvm

如果我用 Java 写这个:

String s = "abc";  

难道s在内存中只占3个字节吗?
如果为真,JVM如何找到String对象的结尾?
这会占用更多内存字节吗?

最佳答案

它需要超过 3 个字节,请继续阅读 this page 的完整解释

For reasons we'll explore below, the minimum memory usage of a Java String (in the Hotspot Java 6 VM) is generally as follows:

Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)

Or, put another way:

  • multiply the number of characters of the String by two;
  • add 38;
  • if the result is not a multiple of 8, round up to the next multiple of 8;
  • the result is generally the minimum number of bytes taken up on the heap by the String.

This page来自同一网站还详细介绍了使用不同于 String

的类来节省内存的方法

关于java - s 在 Java 中占用 3 个字节吗?当: String s = "abc"; Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447819/

相关文章:

javascript - 在 JavaScript 中的 Thymeleaf 转义表达式中使用 Spring Security

node.js - Node js + mongoose,内存不足,耗时过多

Android,xhdpi - 堆

Android:查找内存泄漏的工具?

java - NSSM - 可靠的服务管理器和 Jetty

java - 在将 java 转换的 List 迭代为不可变时使用 scala 并行性

java - 使用反射从 Bundle 获取数据

assembly - 两个连续的标签是否形成两个不同的基本 block 或者它们在 assembly 中是否相同

android - 为什么Android AOT编译只在安装时进行

java - 将带有通配符的 Java 泛型编译为 C++ 模板