衡量应该使用堆中多少内存来创建特定类型的新对象的正确方法是什么(让我们谈谈整数以保持简单)?
这个值可以不用实验计算出来吗?这种情况下的规则是什么?这些规则是否在某处严格指定,或者它们可能因 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/