如何测试对象引用在Java中使用的字节数? [英] How to test how many bytes an object reference use in Java?

查看:172
本文介绍了如何测试对象引用在Java中使用的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试对象引用在我正在使用的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?

谢谢!

推荐答案

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

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.

我假设您在询问如何判断对象占用的空间大小。你可以使用Instrumentation(不是一件简单的事情),但这只会给你一个很浅的深度。 Java倾向于你闯入许多对象,C ++可能是一个单独的结构,所以它没有那么有用。

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.

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

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.

如果你不想使用分析器,另一种获取对象大小的方法是分配一个大型数组并查看消耗了多少内存,你必须多次这样做才能获得一个好主意是什么平均大小。我会将年轻空间设置得很高,以避免GCs混淆你的结果,例如: -XX:NewSize = 1g

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

这篇关于如何测试对象引用在Java中使用的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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