Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test how many bytes an object reference use in Java?

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!

like image 696
Phil Avatar asked Apr 02 '11 02:04

Phil


People also ask

How many bytes are there in a Java object?

Due to the nature of memory (word size) structure, any memory is multiple of 8 and if it's not the system will automatically add additional bytes (but the minimal size is still 8 and 16 bytes for 32/64 systems) Java object has no fields inside and according to specification, it has only metadata called header.

What is the size of object reference in Java?

The size of an object reference depends on the JVM and machine architecture. Generally, on a 32-bit machine it is 32 bits and on a 64-bit machine it is 64 bits.

How much memory does it take to reference an object?

The bottom line is if we disable the compressed references explicitly or the heap size is more than 32 GB, the object references will consume 8 bytes. Now that we know the memory consumption for basic data types, let's calculate it for more complex objects. 5. Complex Objects

What is the size of a course object in Java?

Course object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 java.lang.String Course.name N/A Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total As shown above, the shallow size is 16 bytes, including a 4 bytes object reference to the name field plus the object header. 5.2.


3 Answers

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

like image 155
Peter Lawrey Avatar answered Oct 24 '22 15:10

Peter Lawrey


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.

like image 39
Mike Samuel Avatar answered Oct 24 '22 15:10

Mike Samuel


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

like image 38
squawknull Avatar answered Oct 24 '22 15:10

squawknull