Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

On 64-bit HotSpot with default compressed oops and compressed class pointers, estimate the size of new Object(), an Integer, and a String holding 5 ASCII characters. How would you verify your estimate?

With compressed oops and compressed class pointers, new Object() is a 12-byte header padded to 16 bytes, an Integer is the same header plus a 4-byte int, exactly 16 bytes, and a String of 5 ASCII characters is about 48 bytes: 24 bytes for the String object with its fields, plus 24 bytes for its byte array, which has a 16-byte header and 5 bytes of data padded up. We verify such estimates with JOL, which prints the real field offsets and sizes chosen by the running JVM.

Why this question is asked

Memory estimates decide real things: how many sessions fit in a cache, whether a data structure needs 2 GB or 8 GB, and whether a design should store objects or primitive arrays. Developers often guess sizes from the fields alone and are off by a factor of two or three, because headers, references and padding are invisible in the source code. Interviewers ask this to see whether we can calculate object sizes from first principles and then check them properly. In this answer, we will recap the rules of object layout, size the three objects step by step, look at how the JVM orders fields, and verify everything with JOL.

1A Quick Recap of the Layout Rules

Three rules decide the size of an object on 64-bit HotSpot with default settings. Every object starts with a 12-byte header: an 8-byte mark word for the hash, GC age and lock state, and a 4-byte compressed class pointer. References to other objects are 4 bytes, thanks to compressed oops, which are on for heaps below about 32 GB. And every object's total size is rounded up to a multiple of 8 bytes, because objects are 8-byte aligned. Arrays add a 4-byte length after the header, so an array's header is 16 bytes.

With those rules we can estimate almost any object on paper.

2Sizing new Object() and Integer

new Object() has no fields, so it is just the 12-byte header. Rounding up to a multiple of 8 gives 16 bytes, and 4 of them are padding.

An Integer has one field, an int value of 4 bytes. The header is 12 bytes, the field fits right after it at offset 12, and 12 plus 4 is exactly 16, so there is no padding at all. That is why boxing is expensive in collections: the number itself is 4 bytes, but each Integer object is 16 bytes, plus a 4-byte reference to it in the collection.

Java
Object o = new Object(); // 12-byte header + 4 bytes padding = 16 bytes
Integer i = 1_000; // 12-byte header + int value (4) = 16 bytes
// (Integer.valueOf caches -128..127, so small values share objects; 1_000 is a new one)
List<Integer> list = new ArrayList<>(1_000_000);
// per element: 4-byte reference in the array + 16-byte Integer = 20 bytes
// int[1_000_000] would need 4 bytes per element

3Sizing a 5-Character String

A String is two objects: the String itself and the byte array that holds its characters. Since Java 9, compact strings store text that fits in Latin-1 as one byte per character, and use two bytes per character only when they must. "hello" is plain ASCII, so it needs 5 bytes of data.

The String object has a 12-byte header and four fields: value, the reference to the byte array, 4 bytes; hash, a cached int, 4 bytes; coder, a byte that says Latin-1 or UTF-16, 1 byte; and hashIsZero, a boolean, 1 byte. That is 12 plus 10, or 22 bytes, rounded up to 24.

The byte array has the 16-byte array header, including its length, followed by 5 bytes of data: 21 bytes, rounded up to 24. Together, "hello" costs 48 bytes for 5 characters of text.

Where the 48 bytes of "hello" go
PartBytesRunning total
String header (mark + class)1212
value (reference to byte[])416
hash (int)420
coder (byte) and hashIsZero (boolean)222
padding224 for the String
byte[] header with length1616
5 Latin-1 bytes + 3 padding824 for the array, 48 in total

4Why Field Order in the Source Does Not Matter

The JVM decides where each field goes, not the order in which we declare them. HotSpot groups fields by size, larger ones first, and fills gaps left by the header or by a superclass with small fields, so as little space as possible is wasted on padding. A class that declares a byte, then a long, then a byte, ends up with the long at an 8-byte aligned offset and the two bytes packed together into a gap, not three separately padded slots.

This is why we should not try to save memory by reordering fields in the source code; the JVM already does it, and its rules can change between versions.

Java
class Mixed { byte a; long b; byte c; }
// Declared order: byte, long, byte
// Typical HotSpot layout:
// 0-11 header
// 12 a (byte)
// 13 c (byte)
// 14-15 gap
// 16-23 b (long)
// Instance size: 24 bytes

5Verifying the Estimates With JOL

JOL, the Java Object Layout library from the OpenJDK project, asks the running JVM for the real layout. It is the right tool because it reflects the exact flags of the JVM it runs on, including compressed oops, alignment and compact headers.

Java
// dependency: org.openjdk.jol:jol-core
System.out.println(ClassLayout.parseInstance("hello").toPrintable());
// java.lang.String object internals:
// OFF SZ TYPE DESCRIPTION VALUE
// 0 8 (object header: mark) ...
// 8 4 (object header: class) ...
// 12 4 byte[] String.value [104, 101, 108, 108, 111]
// 16 4 int String.hash 0
// 20 1 byte String.coder 0
// 21 1 boolean String.hashIsZero false
// 22 2 (object alignment gap)
// Instance size: 24 bytes
System.out.println(GraphLayout.parseInstance("hello").totalSize()); // 48

Notice that GraphLayout follows references, so it reports the String plus its array, 48 bytes, while ClassLayout shows only the String itself. For large structures, GraphLayout is how we check an estimate of a whole map or list.

6Follow-up questions

How would compact object headers change these numbers?

With -XX:+UseCompactObjectHeaders on JDK 25, the header shrinks from 12 to 8 bytes. new Object() can become 8 bytes. An Integer becomes 8 plus 4 equals 12, which still rounds up to 16. The String object becomes 8 plus 10 equals 18, still 24 after rounding, and the byte array header becomes 12 bytes, so 12 plus 5 equals 17, still 24. So "hello" stays at 48 bytes, while objects whose size crosses an 8-byte boundary, such as a class with two int fields going from 24 to 16 bytes, shrink. The exact numbers should be checked with JOL on the JVM in question.

What changes with a heap above 32 GB?

Compressed oops are turned off, so every reference becomes 8 bytes. The header stays 12 bytes, because compressed class pointers work independently in current JDKs, but a String's value field grows to 8 bytes, making the String object 12 plus 14 equals 26, rounded to 32 bytes.

Why does String keep both hash and hashIsZero?

hash caches the result of hashCode(). A cached value of 0 could mean either not computed yet or a real hash of zero, so hashIsZero records the second case and avoids recomputing the hash of strings whose hash really is zero.

7Conclusion

In this answer, we saw that with compressed oops and class pointers, new Object() is a 12-byte header padded to 16 bytes, an Integer is 16 bytes with no padding, and a 5-character ASCII String costs 48 bytes: 24 for the String with its value, hash, coder and hashIsZero fields, and 24 for its byte array, thanks to compact strings storing one byte per Latin-1 character. The JVM orders fields to minimise padding, and JOL verifies the estimates on the running JVM.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from the layout rules: a 12-byte header, made of an 8-byte mark word and a 4-byte compressed class pointer; 4-byte references because of compressed oops; and every object rounded up to a multiple of 8.

Then, if they want more

So new Object is 12 bytes of header padded to 16. An Integer adds one 4-byte int right after the header, which makes exactly 16 bytes with no padding. A String is two objects.

The String itself has the header plus a reference to its byte array, the cached int hash, a coder byte and a hashIsZero boolean, so 12 plus 10 is 22, padded to 24.

Since Java 9, compact strings store Latin-1 text as one byte per character, so the array for five ASCII characters is a 16-byte array header including the length, plus 5 bytes, which is 21, padded to 24. That makes 48 bytes for five characters.

I would also mention that the JVM chooses the field order itself, grouping fields by size and filling gaps, so declaration order does not matter. To verify, I would use JOL: ClassLayout.parseInstance prints the offsets, sizes and padding for one object on the running JVM, and GraphLayout gives the total including referenced objects.

That matters because flags such as a large heap or compact headers change the numbers.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "On 64-bit HotSpot with default compressed oops and compressed class pointers, estimate...": https://scrivio-interview-library.netlify.app/java/memory-layout/96405b5bc3/