Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

A reporting service generating large Excel files causes repeated OOM kills in Kubernetes despite using SXSSF. What else could be using memory and disk, and how do you fix it?

SXSSF only keeps the workbook's rows out of the heap; it moves them to temp files and does nothing about the rest. In Kubernetes, java.io.tmpdir may be an emptyDir volume with medium Memory, so those temp files count against the container's memory limit, and undisposed temp files leak after failures. Meanwhile the database driver may load the whole result set, several exports run at once on one pod, and styles or shared strings accumulate. We fix it by moving temp files to a disk-backed volume, cleaning up in finally, streaming the query, limiting concurrent exports per pod, and reusing styles.

Why this question is asked

An OOM kill in Kubernetes is confusing the first time we see it, because the JVM often never reports an OutOfMemoryError. The pod just restarts with exit code 137, and the heap graphs look fine. When the service already uses SXSSF, the team feels it has done the right thing, so the cause must be somewhere less obvious. Interviewers ask this scenario because it tests how well we understand container memory, not only Java heap. In this answer, we will separate heap problems from container kills, then go through each thing that uses memory or disk in an export, and finish with a checklist of fixes.

1A Quick Recap: OOM Kill Versus OutOfMemoryError

A container in Kubernetes has a memory limit, enforced by the Linux kernel through cgroups. The kernel counts everything the container's processes use: the Java heap, but also metaspace, thread stacks, direct buffers, native memory used by libraries, and pages of files in memory-backed filesystems. When the total crosses the limit, the kernel kills the process. Kubernetes shows the container as OOMKilled with exit code 137.

A java.lang.OutOfMemoryError is different: the JVM itself ran out of heap within the -Xmx it was given and threw an error. So the first step is to look at kubectl describe pod and the logs. If we see OOMKilled and no OutOfMemoryError, the heap was not the problem, or not the only one: something outside it pushed the container over the limit.

Shell
kubectl describe pod report-7d9f-abc
# Last State: Terminated
# Reason: OOMKilled
# Exit Code: 137
# JVM sized at 75% of a 2 GiB limit leaves only 512 MiB for everything else:
JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75 -XX:+HeapDumpOnOutOfMemoryError"

2Temp Files in a Memory-Backed Volume

SXSSF works by writing old rows to temporary files in java.io.tmpdir, which is /tmp by default. That is fine on a normal server, where /tmp is on disk. In Kubernetes, many teams run containers with a read-only root filesystem for security, and mount an emptyDir volume at /tmp so the application can still write there. If that emptyDir is declared with medium: Memory, it is a tmpfs: a filesystem in RAM, whose pages are charged to the container's memory.

So the streaming writer moved the data from the heap into the same memory budget by another door. A 500,000-row export can produce hundreds of megabytes of temp files, which is enough on its own to cross a 2 GiB limit when added to the heap. The fix is to mount a disk-backed volume for temp files and point java.io.tmpdir at it, and to give that volume a size limit so a leak cannot fill the node's disk.

YAML
volumes:
- name: export-tmp
emptyDir:
medium: Memory # tmpfs: counts against the container's memory limit
# Fixed: disk-backed, with a size limit
volumes:
- name: export-tmp
emptyDir:
sizeLimit: 5Gi # on node disk, counted as ephemeral storage
containers:
- env:
- name: JAVA_TOOL_OPTIONS
value: "-Djava.io.tmpdir=/export-tmp"
volumeMounts:
- { name: export-tmp, mountPath: /export-tmp }

Even a disk-backed emptyDir counts against the pod's ephemeral storage, and a pod that exceeds its ephemeral-storage limit is evicted. So disk use still needs cleanup; it just no longer causes memory kills. A dedicated persistent volume is another option for very large exports.

3Temp Files That Are Never Cleaned Up

SXSSF deletes its temp files only when we dispose of the workbook. If the export code calls dispose() only at the end of the happy path, every export that fails, times out or is cancelled leaves its temp files behind. Each failure leaks a few hundred megabytes, and on a memory-backed /tmp that looks exactly like a slow memory leak followed by a kill.

Java
SXSSFWorkbook wb = new SXSSFWorkbook(100);
try (wb; OutputStream out = Files.newOutputStream(target)) {
writeRows(wb);
wb.write(out);
} finally {
wb.dispose(); // runs after success, failure and cancellation
}
// and on startup or on a schedule: delete files in /export-tmp older than a few hours

A small startup cleanup of old files in the temp directory is a useful safety net, because a pod killed in the middle of an export never reaches its finally block.

4The Result Set and the Output Buffer

The workbook may stream, but the data feeding it may not. By default the PostgreSQL JDBC driver reads the entire result set into memory before returning the first row. For 2 million rows that alone can be gigabytes of heap. The driver streams only when the connection is not in autocommit mode and a fetch size is set. MySQL's driver has its own rules for streaming (check your driver's documentation). With JPA, a list-returning repository method also loads everything, plus a managed entity per row in the persistence context.

So we stream the source too: a JDBC fetch size such as 5,000 inside a read-only transaction, a Stream-returning repository method with a fetch size hint and entities detached as we go, or keyset pagination in pages of a few thousand rows. One more place to check is the end of the pipeline. Code that writes the workbook into a ByteArrayOutputStream to upload it afterwards holds the entire finished file in heap; writing to a file on the disk volume and uploading from there avoids it.

5Concurrency, Styles and Shared Strings

Each export has its own working memory: the row window, the database fetch buffer, styles, and the upload buffer. One export might use 300 MiB; four running at once on the same pod use 1.2 GiB. Request-driven exports have no natural limit, so a burst at month-end kills the pod. We limit concurrent exports per pod with a small fixed thread pool or a semaphore, queue the rest, and scale out with more pods instead of more threads per pod.

Finally, the workbook's shared tables are not streamed. Creating a CellStyle per cell grows the styles table, and if the workbook was created with the shared strings option, every distinct string is kept in memory until the end. We create each style once and reuse it, and use SXSSF's default inline strings for large exports.

What uses memory and disk in an export pod
ConsumerCounted asFix
SXSSF temp files on tmpfs /tmpContainer memoryDisk-backed volume for java.io.tmpdir
Temp files left after failuresMemory or ephemeral diskdispose() in finally, startup cleanup
Full result set in the driverHeapFetch size, streaming, keyset pages
Finished file in a byte arrayHeapWrite to disk, upload from file
Several exports at onceHeap and temp spaceSemaphore per pod, queue the rest
Style per cell, shared stringsHeapReuse styles, inline strings
Non-heap JVM memoryContainer memoryLeave headroom below the limit

6Follow-up questions

How do you confirm where the memory goes?

Compare the container's working set memory from the metrics with the JVM's heap and non-heap usage from Micrometer or JMX. If the container grows while the heap stays flat, look at tmpfs usage with df inside the pod and at native memory with the JVM's Native Memory Tracking. If the heap grows, take a heap dump during an export and look for result sets or byte arrays.

Why not just raise the memory limit?

It can buy time, but it does not fix a leak or an unbounded number of concurrent exports; the next bigger report or busier day hits the new limit. Raising it is reasonable only after we know the per-export cost and have limited concurrency, so the limit is a calculation rather than a guess.

What heap percentage should a container JVM use?

Usually 50 to 75 percent of the limit, leaving room for metaspace, threads, direct buffers and native libraries. Services that use much native memory or tmpfs need more headroom, which is another reason to keep temp files off memory-backed volumes.

7Conclusion

In this answer, we saw that SXSSF keeps rows off the heap but not out of the container's memory budget. Temp files on a memory-backed emptyDir count against the limit, undisposed temp files leak after failures, the database driver may load the entire result set, concurrent exports multiply memory, and styles and shared strings grow in the heap. Moving temp files to a disk-backed volume, cleaning up in finally, streaming the query, writing the output to disk, limiting exports per pod and reusing styles turns the OOM kills into a predictable, bounded memory profile.

What to say in the interviewAbout 80 seconds aloud

Open with

First I'd confirm it's a container OOM kill, exit code 137 with no OutOfMemoryError, which means the total container memory crossed the limit, not just the heap. SXSSF moves rows into temp files in java.io.tmpdir, and in Kubernetes /tmp is often an emptyDir with medium Memory because the root filesystem is read-only; that's tmpfs, so the temp files count against the container's memory.

Then, if they want more

I'd point tmpdir at a disk-backed volume with a size limit. Then I'd check cleanup: dispose has to run in a finally block, otherwise every failed export leaks its temp files, and I'd add a startup cleanup for pods that were killed mid-export.

Next the data source: the PostgreSQL driver loads the whole result set unless autocommit is off and a fetch size is set, so I'd stream with a fetch size or keyset pages, and I'd make sure the finished file is written to disk, not a ByteArrayOutputStream.

Then concurrency: several exports on one pod multiply memory, so I'd limit them per pod with a semaphore and queue the rest. Finally I'd reuse cell styles, keep inline strings, and leave headroom between the heap size and the container limit.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "A reporting service generating large Excel files causes repeated OOM kills in Kubernetes...": https://scrivio-interview-library.netlify.app/java/document-libraries/2ead9c2f56/