在Java中为一个Integer对象分配了多少内存?如何找出任何自定义对象的这个值? [英] How much memory is allocated for one Integer object in Java? How to find out this value for any custom object?

查看:275
本文介绍了在Java中为一个Integer对象分配了多少内存?如何找出任何自定义对象的这个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测量堆中的内存应该用于创建某种类型的新对象的正确方法是什么(让我们谈谈Integers以保持简单)?

What is the proper way to measure how much memory from the heap should be used to create new object of a certain type (let's talk about Integers to keep it simple)?

可以在没有实验的情况下计算此值吗?那种情况下的规则是什么?这些规则是否在某处严格指定,或者它们可以在jvm到jvm之间变化?

Can this value be calculated without experiment? What are the rules in that case? Are these rules strictly specified somewhere or they can vary from jvm to jvm?

推荐答案

它可能会有所不同,从JVM到JVM。

It could vary from JVM to JVM.

您可能希望此博客文章来自Oracle工程师:

You may like this blog post from an Oracle engineer:


对于32位Hotspot JVM上的Java Integer,32位有效负载(整数.value field)伴随着96个附加位,一个标记,一个klass和一个对齐填充字,总共128位。此外,如果世界上有(例如)六个对此整数的引用(线程加堆),那些引用也占用192位,总共320位。在64位机器上,一切都是两倍大,至少目前是:对象中的256位(现在包括96位填充)和其他地方的384位。相比之下,未装箱的原始整数的六个副本占用192位

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中为一个Integer对象分配了多少内存?如何找出任何自定义对象的这个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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