如何测试Java中对象引用使用了多少字节?

发布于 2024-10-28 10:13:28 字数 63 浏览 1 评论 0原文

我想测试我正在使用的 Java VM 中对象引用使用了多少字节。你们知道如何测试这个吗?

谢谢!

I would like to test how many bytes an object reference use in the Java VM that I'm using. Do you guys know how to test this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笔一画续写前缘 2024-11-04 10:13:28

从字面上看,在大多数 JVM 上,32 位 JVM 上的所有引用都占用 4 个字节,在一个 64 位 JVM 上,引用占用 8 个字节,除非使用了 -XX:+UseCompressedOops,在这种情况下,它占用 4 个字节。

我假设您问如何知道对象占用了多少空间。您可以使用 Instrumentation(不是一件简单的事情),但这只会给您带来浅层深度。 Java 倾向于分解许多对象,而 C++ 可能是单个结构,因此它没有那么有用。

但是,如果您有内存问题,我建议您使用内存分析器。这将为您提供浅层和深层空间物体的使用情况,并为您提供整个系统的图片。这通常更有用,因为您可以从最大的消费者开始并优化它们,即使您已经开发 Java 十年+,您也只会猜测哪里是最佳优化位置,除非您有硬数据。

如果您不想使用探查器,则获取对象大小的另一种方法是分配一个大数组并查看消耗了多少内存。您必须多次执行此操作才能很好地了解平均大小是多少。我会将年轻空间设置得非常高,以避免 GC 混淆您的结果,例如 -XX:NewSize=1g

Taking the question literally, on most JVMs, all references on 32-bit JVMs take 4 bytes, one 64-bit JVMs, a reference takes 8 bytes unless -XX:+UseCompressedOops has been used, in which case it takes 4-bytes.

I assume you are asking how to tell how much space an Object occupies. You can use Instrumentation (not a simple matter) but this will only give you a shallow depth. Java tends you break into many objects something which is C++ might be a single structure so it is not as useful.

However, ifyou have a memory issue, I suggest you a memory profiler. This will give you the shallow and deep space objects use and give you a picture across the whole system. This is often more useful as you can start with the biggest consumers and optimise those as even if you have been developing Java for ten years+ you will only be guessing where is the best place to optimise unless you have hard data.

Another way to get the object size if you don't want to use a profiler is to allocate a large array and see how much memory is consumed, You have to do this many times to get a good idea what the average size is. I would set the young space very high to avoid GCs confusing your results e.g. -XX:NewSize=1g

扛刀软妹 2024-11-04 10:13:28

不同 JVM 的大小可能有所不同,但 "Sizeof for Java”

您可能还记得“Java 技巧 130:您知道您的数据大小吗?”该文档描述了一种基于创建大量相同类实例并仔细测量 JVM 使用的堆大小所导致的增加的技术。当适用时,这个想法非常有效,事实上我将使用它来引导本文中的替代方法。

It can differ from JVM to JVM but "Sizeof for Java" says

You might recollect "Java Tip 130: Do You Know Your Data Size?" that described a technique based on creating a large number of identical class instances and carefully measuring the resulting increase in the JVM used heap size. When applicable, this idea works very well, and I will in fact use it to bootstrap the alternate approach in this article.

淡淡绿茶香 2024-11-04 10:13:28

如果您需要相当准确,请查看仪器框架

If you need to be fairly accurate, check out the Instrumentation framework.

┼── 2024-11-04 10:13:28

这个是我使用的。一定会喜欢那些 16 字节引用!
alphaworks.ibm.heapanalyzer

This one is the one I use. Got to love those 16-byte references !
alphaworks.ibm.heapanalyzer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文