Fundamentals 15 min read

Understanding Java Object Memory Layout and the Oop‑Klass Model

This article explains the JVM Oop‑Klass model, details how Java objects are represented in heap memory, shows how to calculate their size using source code analysis, HSDB, Instrumentation, Unsafe, and third‑party tools, and provides practical examples and code snippets.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Java Object Memory Layout and the Oop‑Klass Model

The article introduces the Oop‑Klass dual‑model used by HotSpot to represent a Java instance, where an instanceOopDesc object holds the instance data and a Klass object holds type metadata.

It describes the two main components of an object header: the mark word (hash code, lock, age, etc.) and the metadata pointer, which may be a compressed or uncompressed Klass reference depending on the -XX:+UseCompressedClassPointers flag.

The layout of the instance data is explained, including field ordering strategies (0, 1, 2) defined by -XX:FieldsAllocationStyle , and the need for padding to keep the total size a multiple of 8 bytes.

Example Java classes ( People , Person ) are used to illustrate how to compute the total heap size: object header (12 bytes) + instance fields (27 bytes) + padding (1 byte) = 40 bytes, plus the sizes of referenced objects (String and char array), resulting in a total of 96 bytes for a Person instance.

The article shows how to verify the calculation with HotSpot Debugger (HSDB): attach to a running JVM, open the Object Histogram, locate the class, and inspect its Oop structure.

Three programmatic approaches for measuring object size are presented:

Using java.lang.instrument.Instrumentation.getObjectSize() – provides an implementation‑specific approximation.

Using sun.misc.Unsafe.objectFieldOffset() to find the maximum field offset and add the field size, then account for padding.

Using third‑party library com.carrotsearch:java-sizeof (class RamUsageEstimator ) to compute size based on JVM specifications.

Sample code snippets are kept intact:

public class Model {
    public static int a = 1;
    public int b;
    public Model(int b) { this.b = b; }
    public static void main(String[] args) throws InterruptedException {
        Model modela = new Model(2);
        Model modelb = new Model(3);
        Thread.sleep(60 * 1000);
        System.out.println("end");
    }
}

Finally, the article concludes that mastering the JVM object layout is essential for tasks such as memory usage estimation, leak detection, and performance tuning.

JavaJVMInstrumentationHSDBmemory layoutUnsafeobject-size
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.