java - Java中为一个Integer对象分配多少内存?如何找出任何自定义对象的这个值?

标签 java performance memory jvm memory-management

衡量应该使用堆中多少内存来创建特定类型的新对象的正确方法是什么(让我们谈谈整数以保持简单)?

这个值可以不用实验计算出来吗?这种情况下的规则是什么?这些规则是否在某处严格指定,或者它们可能因 jvm 而异?

最佳答案

它可能因 JVM 而异。

您可能会喜欢 this blog post来自 Oracle 工程师:

In the case of a Java Integer on a 32-bit Hotspot JVM, the 32-bit payload (a Integer.value field) is accompanied by a 96 additional bits, a mark, a klass, and a word of alignment padding, for a total of 128 bits. Moreover, if there are (say) six references to this integer in the world (threads plus heap), those references also occupy 192 bits, for a total of 320 bits. On a 64-bit machine, everything is twice as big, at least at present: 256 bits in the object (which now includes 96 bits of padding), and 384 bits elsewhere. By contrast, six copies of an unboxed primitive integer occupy 192 bits

关于java - Java中为一个Integer对象分配多少内存?如何找出任何自定义对象的这个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4031242/

相关文章:

将 "int* pointer"与常量内存地址进行比较

java - 使用 SSH 从 Java 应用程序连接到 Hadoop

java - 将 JBoss Maven 存储库中的所有 Artifact 添加到 Eclipse 中的 Maven 项目

c# - 测试c#邮件发送速度

iphone sqlite3对象分配内存增加但没有泄漏

c - 用数组模拟列表

java - Sonar 服务器无法启动,依赖项不满足 : class java. lang.String

java - 查找运行 web 服务的 android 设备的 IP 地址

performance - Haskell:不同功能组成的性能差异?

MySQL 更新查询不使用索引列