Skip to main content

Posts

Showing posts from January, 2014

Dissecting Java object layout and sizeof() computation

This write up discusses how a rough mental calculation (close to accurate) can be performed to compute a java object size (shallow and deep). Target Java Virtual Machine: Hotspot 1.7.0_25 64 bit and Hotspot 1.7.0_51 32 bit Platform: Windows 7 64 bit,  Intel Core i5 Sandy Bridge Code: Pick this single class from my GitHub repo: [JavaObjectLayout] This code uses a third party library classmexer which uses Instrumentation to compute shallow and deep sizes of an object. The class I've written is to verify if this library is returning a correct size or not. class-mexer answers "what" is the size of an object, it does not explain "why". This write-up is an attempt to find that reason.  JavaObjectLayout uses sun.misc.Unsafe to sneak into an object's heap layout. The class is not general purpose but the approach I have applied can be followed to dissect any java object. You'd see use of absolute offsets to fetch a memory