Menu

File size is the wrong number for sizing a Java heap
📰

File size is the wrong number for sizing a Java heap

DEV Community: java·IDRSolutions·about 10 hours ago
#nwtLSdA1
#devtojava#devto#dev#heap#container
Long article (589 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.