计算 Java 对象的字节大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3983360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:15:37  来源:igfitidea点击:

Calculating byte-size of Java object

javaserializationinstrumentation

提问by codeObserver

I am working on calculaitng the size [memory used] of a java object [hashmap] . It contains elements of different data types [at runtime] so [ no-of-elem * size-of-element] is not that good an approach. The code right now does it by series of

我正在计算 java 对象 [hashmap] 的大小 [使用的内存]。它包含 [在运行时] 不同数据类型的元素,因此 [ no-of-elem * size-of-element] 不是一种好的方法。现在的代码通过一系列

if (x)
  do something
else if (primitives)
  lookup size and calculate

However this process is a CPU hog and in-efficient.

然而,这个过程是一个 CPU hog 和低效的。

I am thinking of following 2 approaches instead:

我正在考虑以下两种方法:

  1. Serialize the object to a buffer and get the size.
  2. Look into java.lang.instrument to get the size
  1. 将对象序列化到缓冲区并获取大小。
  2. 查看 java.lang.instrument 以获取大小

I am looking for anyones experience with these approaches for performance , efficiency, scaling etc OR if you know any better way.

我正在寻找任何人对这些方法的性能、效率、扩展等方面的经验,或者如果您知道任何更好的方法。

P.S: This is a background utility that I am building so the size need no be super accurate though it should be about correct. So I am willing to trade accuracy for performance

PS:这是我正在构建的后台实用程序,因此大小不需要非常准确,尽管它应该是正确的。所以我愿意用准确性来换取性能

I am not interested in the deep-size [the size of objects that are refered by this object will not be computed.]

我对深度大小不感兴趣 [不会计算此对象引用的对象的大小。]

I am looking for a performance comparisons and understanding how getObjectSize() works internally ..so that I do not messup something else to improve the performance

我正在寻找性能比较并了解 getObjectSize() 如何在内部工作..这样我就不会搞砸其他东西来提高性能

Thanks

谢谢

回答by Mihir Mathuria

Use getObjectSize()method of the Instrumentation package.

使用Instrumentation 包的getObjectSize()方法。

Look herefor implementation details:

看看这里的实现细节:

回答by mike g

The serialized size is definitely not the way to go, for two reasons:

序列化大小绝对不是办法,原因有二:

  • In the standard java serialization there can be quite a lot of overhead which would add to the size.
  • It would not be any quicker than using the getObjectSize()method which we can presume will iterate over all the references, and use some kind of lookup to determine the size of the primitive values/references of an object.
  • 在标准的 java 序列化中,可能会有很多开销会增加大小。
  • 它不会比使用getObjectSize()方法更快,我们可以假设它会遍历所有引用,并使用某种查找来确定对象的原始值/引用的大小。

If you need better performance then that really will depend on the distribution of your objects. One possiblility would be to do some random sampling of the values in your map, determine an average and calculate an estimate from this value.

如果您需要更好的性能,那么这真的取决于您的对象的分布。一种可能性是对地图中的值进行一些随机抽样,确定平均值并根据该值计算估计值。

For advice on how to look up a random value in a hashmap, see this question.

有关如何在哈希图中查找随机值的建议,请参阅此问题

回答by Neil Coffey

You may be interested in an article I wrote a while ago on how to calculate the memory usage of a Java object. It is admittedly aimed primarily at 32-bit Hotspot, although much of it applies in essence to other environments.

您可能对我前段时间写的一篇关于如何计算 Java 对象的内存使用情况的文章感兴趣。诚然,它主要针对 32 位 Hotspot,尽管其中大部分本质上适用于其他环境。

You can also download a simple agent for measuring Java object sizefrom the same site which will take some of the hard work out of it for you and should work in 64-bit environments.

您还可以从同一站点下载一个用于测量 Java 对象大小的简单代理,这将为您省去一些繁重的工作,并且应该可以在 64 位环境中工作。

Note as others have I think mentioned that the serialised form of an object isn't the same as its form in memory, so using serialisation isn't suitable if you want to measure the memory footprint accurately.

请注意,正如其他人所说,我认为对象的序列化形式与其在内存中的形式不同,因此如果您想准确测量内存占用,则不适合使用序列化。