When a Java service runs out of memory, the first assumption is usually a leak. A leak has a recognisable shape on the heap graph: a sawtooth that never quite returns to baseline, a slow climb over days. Most of the incidents I’ve been asked to look at had a different shape. Memory sat flat for hours, went vertical on one request, and the service fell over. Somebody had sent a file, and on disk it was a few megabytes.
What a decoded image costs
Once decoded, an image costs width times height times bytes per pixel, whatever the file size was. A 6000 by 4000 photo is 96 MB in a BufferedImage at 4 bytes per pixel, from an 8 MB JPEG. An A4 PDF page rendered at 300 DPI is about 35 MB, from a file that might be 50 KB. Converting colour spaces, rotating or scaling usually allocates a second buffer while the first is still live, so one operation can briefly need double.
The default heap is a quarter of the container
With no -Xmx, the JVM caps the heap at 25% of the container’s memory limit. A 2 GB container gets about 512 MB, which fits two of those photos with working copies.
Older JDKs can’t read cgroup v2 limits, which most current Linux distributions use. Support arrived in JDK 15 and was backported to 11.0.16 and 8u372. Earlier builds size the heap from the host’s memory, so it can end up larger than the container. Java 17 and later are fine. Running java -XshowSettings:vm -version inside the container shows what the JVM actually picked.
Concurrency multiplies it
Worker pools sized by core count assume small tasks. Eight workers each holding a 96 MB image and a copy need about 1.5 GB at once. It passes every test because test fixtures are small. Bounding work by estimated memory, with a bounded queue or a semaphore, fixes more of these than heap tuning does.
Two ways to die
Heap exhaustion throws an OutOfMemoryError with a stack trace, and -XX:+HeapDumpOnOutOfMemoryError adds a dump. Container exhaustion means the kernel sends SIGKILL and the process exits with 137, leaving nothing to debug. Setting -Xmx to the full container limit turns the first into the second, because code cache, direct buffers, GC bookkeeping, metaspace and thread stacks all live outside the heap. Direct memory alone defaults to the same limit as the heap. Start -XX:MaxRAMPercentage around 70 to 75 and measure.
Untrusted files
Image headers declare their own dimensions and decoders trust them. A single-color 20,000 by 20,000 PNG is about a megabyte on disk and over a gigabyte decoded. Read the dimensions from the header first (ImageIO’s ImageReader can do this without decoding pixels) and reject or subsample anything over a pixel limit. A limit in megabytes won’t catch this file.
What the fixes cost
Bigger heaps cost money per instance. Capped concurrency costs throughput. Strip or tile decoding helps most but depends on the API and the format, and ImageIO’s convenience methods return the whole image regardless. Subsampling costs detail, which matters if the output goes through OCR.
Before it ships
Know four numbers: how many inputs decode at once, the largest input in pixels or pages, the max heap the JVM picked in the real container, and what’s left outside the heap. Most teams find them after the first 137.
Disclosure: I work at a company that builds pure Java libraries for image and PDF processing. The arithmetic is the same whichever library you use.

