Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

What are humongous objects in G1, and why might a service that builds 20 MB byte arrays perform poorly with 4 MB regions?

In G1, an object of at least half a region is humongous: it skips eden and is allocated directly in one or more contiguous regions of its own. A 20 MB byte array with 4 MB regions needs six contiguous regions, because the array header pushes it just past five, so nearly a whole region is wasted for every array. Many such allocations start concurrent cycles early, fragment the heap so contiguous space is hard to find, and can end in full GCs; we fix it by streaming or chunking the data, or by raising G1HeapRegionSize.

Why this question is asked

Services that build large buffers, such as a report renderer, an export endpoint or a file upload that is read fully into memory, often behave fine in testing and then show odd GC behaviour in production: concurrent cycles that start too often, rising heap use and the occasional full GC. Very often the cause is humongous objects. Interviewers ask this to see whether we understand how G1's region layout meets real allocation patterns. In this answer, we will recap regions, see how a humongous allocation works with real numbers, look at why it hurts, how G1 tries to reclaim such objects, and how to fix the service.

1A Quick Recap of G1 Regions

G1 divides the heap into equal regions. By default the region size is chosen so the heap has about 2,048 regions, rounded to a power of two between 1 MB and 32 MB; JDK 18 and later allow up to 512 MB when set explicitly. A service with an 8 GB heap therefore gets 4 MB regions.

Normal objects are allocated in eden regions, usually through a thread-local allocation buffer, a TLAB, which is a small chunk of eden that one thread can fill without locking. They are collected by copying them during young collections. That model works well for small objects, which are almost all objects.

2What Makes an Object Humongous

Copying a very large object at every young collection would be expensive, and it would not fit nicely in a region with other objects anyway. So G1 treats any object of at least half a region as humongous. With 4 MB regions, that is anything of 2 MB or more. A humongous object is allocated directly into a run of free, contiguous regions, outside the young generation, and is not moved during normal collections. Let us count what our 20 MB array needs.

Java
int size = 20 * 1024 * 1024; // 20,971,520 bytes of data
byte[] report = new byte[size]; // plus a 16-byte array header = 20,971,536 bytes
long region = 4L * 1024 * 1024; // 4,194,304 bytes per region
long regions = (20_971_536 + region - 1) / region; // 6: five regions hold 20,971,520 bytes, 16 bytes short
long wasted = regions * region - 20_971_536; // 4,194,288 bytes, almost a whole region
Heap regions after allocating one 20 MB array
RegionContentUsable by others?
R100humongous start: header + first 4 MBno
R101 - R104humongous continuation: next 16 MBno
R105last 16 bytes of the arrayno, about 4 MB wasted
R106freeyes

Notice how unlucky this size is. The data fills exactly five regions, the 16-byte header spills into a sixth, and the rest of that sixth region cannot be used by any other object. Every such array costs 24 MB of heap for 20 MB of data, and it needs six free regions next to each other.

3Why Humongous Allocations Hurt

The first problem is timing. Humongous objects count as old-generation occupancy the moment they are allocated, and each humongous allocation makes G1 check whether it should start concurrent marking. A service that creates ten of these arrays per second adds 240 MB per second of old occupancy, so marking cycles start far more often than the real live data would justify. In the log this shows up as Pause Young (Concurrent Start) (G1 Humongous Allocation).

The second problem is fragmentation. A humongous object needs a run of contiguous free regions. After a while the heap may have plenty of free regions in total but no run of six in a row. When G1 cannot find one, it tries a collection, and if that does not help, it falls back to a full GC, which compacts the heap so that free regions are contiguous again. That full GC is the multi-second pause the team eventually notices.

The third problem is waste. Our example loses one sixth of every allocation. With 50 such arrays alive at once, that is 200 MB of heap holding nothing.

4Eager Reclaim Helps, but Only Somewhat

Since JDK 8u60, G1 can reclaim dead humongous objects during a young collection instead of waiting for marking. It does this when it can cheaply prove that nothing references the object, which is easiest for primitive arrays like our byte[] because they contain no references themselves; recent JDKs have extended the cases it covers, so check your version.

This is why a short-lived 20 MB buffer often disappears at the next young pause. But it does not remove the other costs: the object was still allocated outside eden, still needed contiguous regions, still wasted a region, and still counted towards the marking threshold while it lived. It is a mitigation, not a design to rely on.

5Fixing the Service

The best fix is to not hold the whole thing in memory. If the service builds a report and then writes it to a response, it can write it in pieces as it produces them.

Java
// Before: the whole report in one 20 MB humongous array
ByteArrayOutputStream out = new ByteArrayOutputStream();
renderer.renderTo(out); // grows to 20 MB, copying as it resizes
response.getOutputStream().write(out.toByteArray()); // and one more 20 MB copy
// After: stream directly, with a small reusable buffer
try (OutputStream os = new BufferedOutputStream(response.getOutputStream(), 64 * 1024)) {
renderer.renderTo(os); // 64 KB buffer: far below the 2 MB threshold
}

Notice that the old version was even worse than one 20 MB array. ByteArrayOutputStream doubles its internal array as it grows, so it allocated humongous arrays of 4, 8 and 16 MB on the way, and toByteArray() made a final copy.

When the data really must be in memory, we can split it into chunks below half a region, for example a list of 1 MB arrays. The last resort is a larger region size with -XX:G1HeapRegionSize. To make 20 MB arrays non-humongous we would need regions bigger than 40 MB, so 64 MB, which on an 8 GB heap means only 128 regions and coarser, less adaptive collections. Raising the region size to 8 or 16 MB can still help by reducing waste and the region count per object, but it is a tuning knob, not a cure.

6Follow-up questions

How do GC logs show humongous allocation?

Three places help. The cause G1 Humongous Allocation appears on pauses that humongous allocations triggered, often Pause Young (Concurrent Start). With -Xlog:gc+heap=info, each pause prints region counts per type, including a line like Humongous regions: 30->6, which shows how many humongous regions existed before and after. Finer detail about eager reclaim is available with the gc+humongous tag at debug level. JFR adds allocation events outside TLABs with stack traces, which tell us which code made the large objects. Exact wording varies across JDK versions.

Is an object exactly half a region humongous?

Yes. The rule is at least half a region, so with 4 MB regions an object of exactly 2 MB, header included, is humongous. Anything smaller is allocated normally in eden.

Do other collectors have the same problem?

Every region-based collector needs some special path for very large objects. ZGC places large objects in pages sized for them, and Shenandoah also has humongous regions. Parallel and Serial allocate large objects in their contiguous generations. The cost profile differs, but streaming instead of buffering helps under all of them.

7Conclusion

In this answer, we saw that G1 treats objects of at least half a region as humongous and allocates them in contiguous regions outside eden. A 20 MB byte array with 4 MB regions needs six regions, because its 16-byte header spills past five, wasting almost 4 MB each time. Humongous allocations start concurrent cycles early and fragment the heap until a full GC is needed, and eager reclaim softens this only for some objects. The fixes are to stream data instead of buffering it, to chunk buffers below half a region, or, as a last resort, to increase G1HeapRegionSize.

What to say in the interviewAbout 90 seconds aloud

Open with

In G1, any object of at least half a region is humongous. Instead of going into eden, it is allocated in its own run of contiguous free regions and is not moved by young collections.

Then, if they want more

Our service probably has an 8 GB heap, so 4 MB regions, and a 20 MB byte array is 20 MB of data plus a 16-byte header, which is just over five regions, so it takes six contiguous regions and wastes almost a whole one. That hurts in three ways.

Humongous objects count as old occupancy straight away, so they trigger concurrent marking cycles far earlier than the real live set would; you see G1 Humongous Allocation as the pause cause. They need contiguous free regions, so the heap fragments, and when G1 cannot find six in a row it may end in a full GC. And they waste space.

G1 can eagerly reclaim dead humongous primitive arrays during young collections, which helps, but the allocation cost and fragmentation remain. The real fix is in the code: stream the data to its destination with a small buffer, or keep it in chunks below 2 MB, and watch for ByteArrayOutputStream, which allocates growing copies.

Raising G1HeapRegionSize is the last resort, since making 20 MB objects normal would need 64 MB regions.

About this answer

Last updated
12 November 2026
Versions it names
Java 18
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 "What are humongous objects in G1, and why might a service that builds 20 MB byte arrays...": https://scrivio-interview-library.netlify.app/java/garbage-collection/e82ac34657/