在哪里可以找到如何计算java对象大小的证据 [英] Where to find the evidence of how to calculate the size of a java object

查看:162
本文介绍了在哪里可以找到如何计算java对象大小的证据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了java对象的大小了很长时间,有很多答案,如这个,每个人都告诉我java对象的开销大小,以及如何计算出实际大小。但他们怎么知道呢?我没有从官方的oracle文件中找到任何证据。这个结论有什么证据?或者数据来自一些基于某些实验的猜测?

I've searched about the size of a java object for a long time, there are a lot of answers like this, everyone is telling me that the size of the overhead of a java object, and how to calculate out the actual size. But how do they know that? I did not find any evidence from the official oracle documents. What is the evidence for that conclusion? Or the data was just come from some guesses based on some experiments?

另一件事。在官方文档中提到,有一种近似的方法来测量对象 - 仪表的方式,任何人都可以向我解释什么是近似的意思?什么时候准确,什么时候不准确。最好有证据。

Another thing. It is mentioned in the official document that there is a 'approximative' way to measure the object - the Instrumentation way, can anybody explain to me what is the 'approximately' means? When it is accurate, when it is not. Better to have the evidence.

推荐答案


如何计算实际大小。但他们怎么知道呢?

how to calculate out the actual size. But how do they know that?

来自经验。


我没有从官方的oracle文件中找到任何证据。

I did not find any evidence from the official oracle documents.

它取决于JVM。对于基于OpenJDK的JVM,32位JVM具有与64位JVM不同的标头大小,因为标头包含引用。其他JVM可能会再次出现不同。

Its up to the JVM. For OpenJDK based JVMs, 32-bit JVM has a different header size to 64-bit JVM as the header contains a reference. Other JVMs could be different again.


或者数据来自某些基于某些实验的猜测?

Or the data was just come from some guesses based on some experiments?

基本上,是的。


可以任何人向我解释什么是近似的意思?

can anybody explain to me what is the 'approximately' means?

当你测量一个物体的大小时,它可能意味着许多不同的东西

When you measure the size of an object it can means many different things


  • 对象有多大? (浅层深度)

  • 它使用了多少内存? (对象分配是8字节对齐,即总是8的倍数)

  • 对象和引用的所有对象使用了多少空间? (深度)

  • 如果被丢弃,可以释放多少空间? (共享多少个对象并且它出现在释放内存的两个片段的中间)

  • 您是否计算堆栈上使用的空间或堆内存中使用的空间?

  • How big is the object? (Shallow depth)
  • How much memory does it use? (Object allocation is 8-byte aligned i.e. always a multiple of 8)
  • How much space does the object and all the objects referenced use? (Deep depth)
  • How much space might be freed if it were discarded? (how many objects are shared and does it appear in the middle of two fragments of freed memory)
  • Do you count the space used on the stack, or space used in off heap memory?

根据你需要知道的情况,你可以得到许多不同的答案,有一个数字大约接近所有用于计算的这些。

Given you can have many difference answers depending on what you need to know, it is useful to have one number which is approximately close to all of these which you use for calculations.

使用Runtime遇到问题的地方是TLAB在大块中分配数据。可以以多线程方式进一步分配这些大块。缺点是你没有获得准确的内存使用信息。

Where you get a problem using Runtime is that the TLAB allocated data in large blocks. These large blocks can be further allocated in a multi-thread way. The downside is you don't get accurate memory used information.

static long memTaken() {
    final Runtime rt = Runtime.getRuntime();
    return rt.totalMemory() - rt.freeMemory();
}

public static void main(String... args) {
    long used1 = memTaken();
    Float i = new Float(0);
    long used2 = memTaken();
    System.out.println("new Float(0) used "+(used2 - used1)+" bytes.");
}

没有期权的运行

new Float(0) used 0 bytes.

关闭TLAB,你会看到 -XX:-UseTLAB

Turn off the TLAB and you see with -XX:-UseTLAB

new Float(0) used 336 bytes.

这比你想象的要高得多,因为必须加载类本身。如果你首先通过添加到开始创建一个Float实例

This is much higher than you might expect because the class itself had to be loaded. If you create one instance of a Float first by adding to the start

Float j = new Float(1);

你得到

new Float(0) used 16 bytes

这篇关于在哪里可以找到如何计算java对象大小的证据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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