Kubernetes Container Gets OOMKilled Even Though Memory Is Still Available
Table of Contents
OOMKilled is a fairly common issue in Kubernetes, but I haven’t seen many articles covering it in detail, so I wanted to share this for anyone who hasn’t run into it yet.
I kept adding a few more ideas to this article every day, so it ended up being quite long. But if you ever need it, at least everything is here.
At first glance, everything may still look fine on the dashboard. The node still has available RAM, but the Pod keeps restarting.
Run kubectl describe pod, and you see:
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
If this is your first time seeing it, your immediate question might be:
Why is the container OOM if the node still has RAM?
This is where things can get confusing.
The container does not get to use all of the free RAM on the node. It can only use memory within its own configured limit.
Simply put:
Free RAM on the Node does not mean free RAM for the container.
So when you encounter OOMKilled, a more accurate question is:
Has the container reached its own memory limit?
What Does OOMKilled Actually Mean?
In simple terms, OOMKilled means a process inside the container was killed by the kernel because it used more memory than it was allowed to use.
In Kubernetes, each container can have something like:
resources:
requests:
memory: "512Mi"
limits:
memory: "1Gi"
You can think of request as telling the scheduler:
This container needs at least this much memory before you place it on a node.
limit is the actual maximum amount of memory the container is allowed to use.
If it exceeds that limit, the container may be killed by the kernel.
A container can use more than its request if the node still has resources available.
But it cannot exceed its limit.
So with a Pod configured like this:
resources:
requests:
memory: "512Mi"
limits:
memory: "768Mi"
the container can still get OOMKilled when memory usage approaches 768Mi, even if the node has plenty of free RAM.
A Common Scenario
Suppose you have an API service with an endpoint for exporting reports.
Normally, it exports only a few thousand rows, so everything works fine.
The Pod is configured like this:
resources:
requests:
memory: "512Mi"
limits:
memory: "1Gi"
Then one day, a user exports a very large report, for example 6 months or 1 year of data.
The application code does something like:
- Fetch all data from the database
- Keep all of it in one large list in RAM
- Generate the Excel/CSV file entirely in RAM
- Only then return the file to the user
Another way to picture it is this:
The container is allowed to use at most 1Gi of memory, but the application tries to fit the entire large report into RAM at once.
The larger the report, or the more export requests running at the same time, the more likely memory is to hit 1Gi and get killed.
The important part is that from the application’s perspective, there may not even be a clear stack trace.
The container just disappears and gets restarted.
And this is where the dashboard can mislead you.
Why Can the Dashboard Be Misleading?
Because an OOM event often happens very quickly, and the container is restarted immediately afterward.
While the export request is running, memory may climb close to 1Gi and then the container gets killed.
But when you open the dashboard afterward, what you are looking at is already the new container after the restart:
- The node still has plenty of RAM
- The Pod currently uses only a few hundred Mi
- Everything looks normal
So it is easy to wonder:
The node still has RAM, so why was the container OOMKilled?
The same thing applies to kubectl top pod after a restart:
kubectl top pod report-api-v2-primary-65f8d -n prod
NAME CPU(cores) MEMORY(bytes)
report-api-v2-primary-65f8d 120m 320Mi
It looks like the Pod is only using 320Mi.
But that is the memory usage of the new container after the restart.
So with OOMKilled, do not look only at the current state.
You need to inspect historical metrics around the restart time to see whether memory previously spiked close to the limit.
Check Whether the Pod Was OOMKilled
Once you know that the current dashboard can be misleading, the first step is to confirm directly in Kubernetes why the Pod restarted:
kubectl describe pod report-api-v2-primary-65f8d -n prod
Look for:
Containers:
report-api:
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Started: Sat, 28 Mar 2026 10:12:14 +0700
Finished: Sat, 28 Mar 2026 10:18:41 +0700
Restart Count: 5
If you want a faster check using jsonpath:
kubectl get pod report-api-v2-primary-65f8d -n prod
-o jsonpath='{.status.containerStatuses[*].lastState.terminated.reason}'
Result:
OOMKilled
Check recent events:
kubectl get events -n prod --sort-by='.lastTimestamp' | grep -i oom
You may see:
Warning OOMKilling pod/report-api-v2-primary-65f8d Container report-api was killed due to memory limit
Not every cluster retains events for a long time.
If the incident happened a while ago, the event may already be gone. In that case, you have to rely on metrics, runtime logs, or monitoring.
Check the Request and Limit
After confirming that the Pod was OOMKilled, the next step is to check how much memory the container is actually allowed to use.
kubectl get pod report-api-v2-primary-65f8d -n prod -o yaml | grep -A8 resources
Example output:
resources:
limits:
memory: 1Gi
requests:
memory: 512Mi
Or inspect the Deployment directly:
kubectl get deploy report-api -n prod -o yaml | grep -A12 resources
There are 3 questions you should answer immediately:
- What is the memory limit?
- Is the application loading a large amount of data into RAM?
- Did the metric reach close to the limit before the restart?
If the limit is 1Gi and the memory graph reaches 980Mi before dropping back to 200Mi after the restart, there is a very high chance this was OOMKilled.
Inspect the cgroup Directly Inside the Container
If you want to investigate further and the current container is still running, you can enter the Pod and inspect the cgroup information.
cgroup is the Linux mechanism used to limit container resources.
Simply put, this is where Linux records how much memory a container is currently using and how much it is allowed to use.
With cgroup v2:
kubectl exec -it report-api-v2-primary-65f8d -n prod -- sh
Inside the container:
cat /sys/fs/cgroup/memory.current
cat /sys/fs/cgroup/memory.max
cat /sys/fs/cgroup/memory.events
Example:
$ cat /sys/fs/cgroup/memory.max
1073741824
$ cat /sys/fs/cgroup/memory.current
934281216
$ cat /sys/fs/cgroup/memory.events
low 0
high 0
max 128
oom 3
oom_kill 3
Read it like this:
memory.max is the cgroup memory limit
memory.current is the current memory usage
oom_kill increasing means a process has been OOM-killed inside the cgroup
If you are using cgroup v1, the paths may look like this:
cat /sys/fs/cgroup/memory/memory.limit_in_bytes
cat /sys/fs/cgroup/memory/memory.usage_in_bytes
cat /sys/fs/cgroup/memory/memory.failcnt
Not every image has all the tools you might want for troubleshooting, but the cgroup files themselves are usually available.
OOMKilled vs Evicted
It is also important to clearly separate these two cases because they can look similar at first.
Case 1: The container exceeds its memory limit
Reason: OOMKilled
Exit Code: 137
This usually means the container was killed because it exceeded its own limit.
Case 2: The Pod is evicted by kubelet because the Node is low on resources
Status: Failed
Reason: Evicted
Message: The node was low on resource: memory.
These are different situations.
- With OOMKilled, the issue is usually the container’s memory usage relative to its limit.
- With Evicted, the issue is node memory pressure, QoS class, request, priority, and overall resource usage on the node.
Check the node:
kubectl describe node <node-name> | grep -A5 -i MemoryPressure
Example:
MemoryPressure False
If MemoryPressure=False but the container was still OOMKilled, the problem is usually the container’s own limit, not the node running out of RAM.
Common Causes
1. The Limit Is Too Low for the Real Workload
This is probably the most common cause.
At first, the application works fine because traffic is low.
Later:
- More users arrive
- Requests become heavier
- Exports get larger
- Cache grows
Memory usage increases, but the limit stays the same.
Then the Pod starts restarting.
Kubernetes did not suddenly break. The maximum memory allocation is simply no longer enough for the current workload.
When debugging, look at:
- Memory before the restart
- Whether traffic increased at that time
- Whether there were export requests, batch jobs, or heavy reports
- Whether memory spiked close to the limit
Do not look only at average memory usage.
OOM often happens during a short spike.
2. The App Keeps Too Much Data in RAM
For example, with report-api, the application exports files like this:
- Fetch all data from the database
- Keep all of it in a list
- Generate the Excel/CSV file in RAM
- Only then return the file to the user
This works fine for small files.
But with a large report, memory usage can grow very quickly.
If several users export reports at the same time, the container can hit its limit even faster.
3. Unbounded Cache
Cache can make an application faster, but an unbounded cache can easily become a problem.
At first, the cache is small and everything works fine.
Over time, the cache grows, memory usage keeps increasing, and eventually the Pod gets OOMKilled.
4. Slow Memory Leak
Another pattern is when memory does not spike suddenly but keeps climbing slowly over time.
For example:
08:00 350Mi
10:00 460Mi
12:00 590Mi
14:00 720Mi
16:00 890Mi
16:20 restart
If the graph looks like this, you should suspect that the application is retaining objects it no longer needs, the cache is not being cleaned up, buffers are not being released, or connections/sessions are being kept for too long.
Increasing the limit may make the Pod survive longer, but it does not fix the root cause.
5. Sidecars Also Use Memory
A Pod does not always contain only the application container.
It may also include:
istio-proxy- log agent
- metrics exporter
So when debugging, check memory usage per container:
kubectl top pod report-api-v2-primary-65f8d -n prod --containers
If you only look at total Pod memory, it is easy to miss which container is actually growing.
Quick Runbook for OOMKilled
When a Pod keeps restarting, do not guess immediately.
Follow these steps and you can usually narrow down the direction fairly quickly.
1. Confirm That It Is Actually OOMKilled
kubectl describe pod
<pod> -n <namespace>
Look for:
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Restart Count: ...
If the reason is Evicted, the debugging path is different.
In that case, check whether the node is low on memory.
2. Check How Much Memory the Container Is Allowed to Use
kubectl get deploy
<deploy> -n <namespace> -o yaml | grep -A12 resources
Write down a few important things:
memory request
memory limit
which container restarted
The important one is memory limit.
If the container has a limit of only 1Gi, then even if the node has available RAM, the container can still be killed when it approaches 1Gi.
3. Check Memory Before the Restart
kubectl top after the Pod has restarted is only useful as a reference because it is showing the new container.
Open your monitoring graphs and compare these 3 things:
container memory usage
container memory limit
restart time
For example:
10:10 memory 420Mi
10:14 memory 760Mi
10:18 memory 980Mi close to the 1Gi limit
10:19 restart
10:20 memory 220Mi new container after restart
This makes the pattern fairly clear.
The old container climbed close to the limit and was killed.
The 220Mi after the restart is simply the memory usage of the new container.
4. Look at the Memory Pattern
Inspect the memory graph around the restart time:
- Sudden spike: suspect a heavy request, file export, batch job, or large payload.
- Gradual increase: suspect a memory leak or unbounded cache.
- Increase following traffic: suspect high concurrency or many requests running at the same time.
For report-api, if the spike happens exactly when a large export request runs, the investigation direction becomes fairly obvious:
The application is keeping too much data in RAM.
5. Temporary Mitigation
If you are in the middle of an incident, stabilize the system first:
- Temporarily increase the memory limit if the node has enough resources.
- Increase the number of replicas if the application can scale horizontally.
- Temporarily reduce or disable heavy export/report endpoints.
- Reduce worker concurrency.
- Reduce page size or limit the export time range.
- Roll back if the OOM started after a new deployment.
For example, with report-api, an emergency workaround could be:
Temporarily limit exports to a maximum of 31 days
Reduce the number of export jobs running at the same time
Increase the limit from 1Gi to 1536Mi if the node has enough resources
But remember:
Increasing the limit only buys you time.
If the application still loads an entire large report into RAM, the Pod can still die again.
Long-Term Fixes
In the long term, adding more RAM should only be treated as a way to buy more time.
If you want the incident to stop recurring, you need to fix whatever makes the application hit its limit so easily.
1. Fix the Export/Report Flow
If the issue comes from report exports, the first thing to review is how the application generates the file.
Avoid loading all data into RAM before returning the file.
Instead, you can:
- Stream chunks of data directly into the file.
- Paginate database reads.
- Limit the export time range, for example to a maximum of 31 days.
- For large reports, move the work to a background job and send the user a download link afterward.
2. Block Overly Heavy Requests Early
The application should protect itself from requests that are too large:
- Limit page size.
- Limit request body size.
- Limit the number of exported rows.
- Limit the number of export jobs running at the same time.
- Return a clear error if the report is too large.
Returning something like:
“Report is too large. Please select a shorter time range.”
is still much better than letting the Pod die and leaving the user with some strange error.
3. Revisit Requests and Limits
Once you understand how the application actually uses memory, then adjust its resources based on:
- Memory p95/p99.
- Peak usage during busy hours.
- Memory usage while generating large reports.
- Whether the node still has enough resources.
If large reports can push memory extremely high, raising the limit is still only temporary.
The root cause is how the application processes the report.
4. Add Early Alerts
Do not wait until the Pod restarts before noticing the problem.
You should consider alerts when:
- Memory usage exceeds 85% of the limit for several minutes.
- Container restart count increases abnormally.
- Last terminated reason is
OOMKilled. - Memory keeps increasing steadily over several hours.
5. Retest with the Same Workload That Caused the Incident
After the fix, rerun the exact workload that previously caused the Pod to fail:
- Export a large report.
- Run multiple export requests at the same time.
- Send higher-than-normal traffic.
- Run a batch job at the same time.
If memory no longer spikes close to the limit, then the fix becomes much more trustworthy.
Conclusion
OOMKilled in Kubernetes should not simply be understood as:
“the server ran out of RAM.”
Sometimes the server still has RAM.
The Node looks fine.
The cluster does not show any major warning.
But the container has already hit its own limit.
So when you encounter OOMKilled, change the question from:
Does the Node still have memory?
to:
Does the container still have memory available within its limit?
Because in Kubernetes, the application does not live inside the entire Node.
It lives within the portion of resources allocated to its container.
And once that portion runs out of memory, the kernel kills the process.
It does not wait for the application to clean up, and it does not care that the Node dashboard still looks perfectly healthy.