Video in progress

Strong Middle ⚙️ JVM and memory

What is a humongous object in G1, and what problems do they cause?

An object larger than half the region size is humongous. It is placed directly into old, in one or several contiguous regions. If there are many of them, the heap fragments, regions are wasted and unexpected Full GCs appear.

📖 Answer

For example, on a 4 GB heap a region is usually 2 MB. So any array larger than 1 MB is humongous: a big byte[] (file upload), reading JSON entirely into a String, the internal array of an ArrayList with thousands of elements.

The problems:

  • a 2.1 MB object occupies two regions, and about 1.9 MB of the second one is empty, but no other object fits in it;
  • contiguous free regions are needed. Even if the heap has free space, a Full GC happens when no contiguous stretch can be found;
  • a short-lived humongous object is still born in old.

How to see it in the log:

Pause Young (Concurrent Start) (G1 Humongous Allocation)

If such causes show up often, turn on -Xlog:gc+humongous=debug for more detail.

The fixes, in the order I apply them:

  1. Change the code: stream big files and responses, don't read a byte[] as a whole, process a big collection in chunks.
  2. Increase the region size: -XX:G1HeapRegionSize=8m (or 16m/32m). The object is then no longer humongous.
  3. Increase the heap.

The good news: in newer JDKs G1 can collect short-lived humongous objects during a young GC too (eager reclaim), but this works well only for arrays that contain no references.