← All posts
Engineering12 min read

Why Your AI SRE Can’t Stop a Java Memory Leak Before the OOMKill

We ran five versions of a slow leak on our own Kubernetes cluster. An observability-first agent, ours on day one, warned zero minutes ahead and raised the limit. Here is what it took to fix the leak before the crash, and a ten-minute test for your own Java services.

Nati ShalomNati ShalomCo-founder & CTO
Why Your AI SRE Can’t Stop a Java Memory Leak Before the OOMKill

We ran five versions of a slow leak on our own Kubernetes cluster. An observability-first agent, ours on day one, warned zero minutes ahead and raised the limit. Here is what it took to fix the leak before the crash, and a ten-minute test for your own Java services.

We set a slow memory leak loose in a Java checkout service on our own Kubernetes test cluster. One Helm value, the cap on an order cache, went from 10000 to 0. The cache stopped evicting, the heap grew a few MiB a minute, and 22.8 minutes later the kernel killed the container at its 512Mi limit. Java never threw an OutOfMemoryError. It never got the chance.

Otto, our agent, behaved like an observability-first AI SRE that day. It gave zero minutes of warning. The first incident opened 2.5 minutes after the crash. The analysis said the root cause was “not yet established”, and the plan raised the memory limit from 512Mi to 768Mi with kubectl patch. That buys time, and then the leak fills the bigger box too.

This is not a legacy problem. The case that started this test was a new AI trading application, written in Java and running on Amazon EKS. Java keeps growing in AI: in Azul's 2026 survey of Java users, 62% said they use Java to build AI functionality, up from 50% a year earlier. And teams hit this all the time. A search of ten subreddits found at least 28 threads since 2018 about Java pods killed or mis-sized for memory, 5 of them in 2026 alone:

I have run into this a lot. At GigaSpaces we built an in-memory database on Java, and many of the financial customers I talk to run on Java too. The garbage collector manages the heap, the operating system manages the container's memory, and each layer thinks it is the one in charge. They often conflict, and that is what makes it so hard to see what is going on.

If you run Java on EKS and watch it in Grafana, you may know this pattern: the memory alert arrives after the pod is gone. This post is about why that happens, why an agent that starts from the alert cannot fix it, and what we changed so the fix lands before the crash. The test at the end takes about ten minutes.

Why standard observability can’t see it coming

Figure 1: Our Java memory demo before the changes. Heap after GC (purple) climbs in steps, the container (green) hits its 512 MiB limit and the kernel kills it (orange line, 31 minutes after the change). The 90% heap rule (dashed orange) never fired, and 49 minutes after the change there was still no incident.
Figure 1: Our Java memory demo before the changes. Heap after GC (purple) climbs in steps, the container (green) hits its 512 MiB limit and the kernel kills it (orange line, 31 minutes after the change). The 90% heap rule (dashed orange) never fired, and 49 minutes after the change there was still no incident.

Our dashboards had every number in Figure 1. None of them warned us, for four reasons.

1. The alert watches a level that is never reached in time. The classic rule says heap above 90% for 10 minutes. A JVM in a container has two ceilings: the heap’s own maximum, and the container’s memory limit, which the kernel enforces. Java uses more than its heap (thread stacks, metaspace, buffers, the JIT), so the container hits 512Mi while the old generation still has room. The kernel kills the pod before Java ever feels full.

2. The number that shows a leak is not collected by default. Heap used saws up and down with every garbage collection. A leak shows in the heap that survives a collection, jvm_gc_live_data_size_bytes in Micrometer, and that gauge was not in our default scrape.

3. Memory charts alone cannot tell a service warming up from one bleeding out. Picture two water tanks at the same level: in one the float valve shuts at the working level, in the other the valve is stuck open and the tank keeps filling until it spills. After every restart a Java service fills its caches. Ours holds up to 10,000 orders of 4 KiB and keeps each receipt for 36 minutes, so on a healthy pod heap after GC climbs for about 40 minutes before it levels off. In our run on 27 November, a healthy pod sat at 68.0 MiB of heap after GC at 20 minutes old, and a leaking pod at the same age at 65.2. They only separated after minute 25. Any rule on that curve alone either fires after every deploy or waits until it is too late.

Figure 2: Heap after GC for a healthy pod warming its caches and a leaking pod, measured on the same service with the same traffic. For 25 minutes they look the same; only the Helm change at minute 0 tells them apart.
Figure 2: Heap after GC for a healthy pod warming its caches and a leaking pod, measured on the same service with the same traffic. For 25 minutes they look the same; only the Helm change at minute 0 tells them apart.

4. The tie-breaker is not in the metrics store. What tells a warm-up from a leak is context: when the pod started, what changed just before the growth (here, Helm revision 22 set the cache cap to 0), and whether traffic moved. That lives in Helm, in Kubernetes and in the request rate, and a person has to put it next to the memory curve. At 3 a.m., nobody does.

A dashboard shows you the clues; it cannot connect them while the service is failing. That is why you need an AI SRE agent, not better dashboards. The evidence that separates a healthy warm-up from a leak sits in three places at once: the heap curve, the Helm history and the traffic. A detective cannot solve a robbery in five minutes if the fingerprints, the camera footage and the suspect list sit in three different police stations. Nobody joins those three by hand every time a pod restarts; an agent does, and that is the only way the warning comes before the crash instead of after it. But an AI SRE that works mostly from observability data inherits all four limits above: it wakes on the same late alert, reads the same curve, and has no Helm history to break the tie. Ours did exactly that on day one, as the next section shows.

What an observability-first AI SRE does with a leak

By observability-first I mean the common design: an agent whose main source is the observability data. It wakes on an alert, reads metrics and logs, and writes an explanation for a person. On day one, its analysis called four tools (pod status, pod metrics, events and logs), and none of them could see the Helm release. Every step was reasonable on its own:

  • It started late. No alert fired before the crash, so it had nothing to start on.
  • It read the symptom. Pod status, events, logs and pod metrics. It never read the Helm release, so it never saw the cache cap go from 10000 to 0.
  • It picked the only safe move left. Without the change history, a bigger limit is the one fix that cannot make things worse today.

That is the trap. The cause sat in one value of Helm revision 22, not in any metric. Telling a leak from a small limit took three reads the alert never carried: heap after GC per pod, the request rate, and the Helm diff for the last hour. An agent that waits for the alert has no time for them, and one that only reads metrics never sees the diff.

Figure
StepObservability-first (our agent on day one)Remediation first (after the changes)
When it startsafter the kernel kill: incident at +25.3 minon the trend, 10 to 25 min before the crash
What it readspod status, events, logs, pod metricsheap after GC per pod, traffic, Helm history and the value diff
What it concludes“root cause not yet established”growth started after Helm revision 22 set the cache cap to 0
What it doeskubectl patch the limit to 768Mihelm rollback to revision 21, as a pull request a person approves
How it endsthe Deployment drifts from Helm; the leak returnspost-flight checks the release and the trend; incident closed

Watch the trend, not the level

How full memory is tells you nothing about how fast it is filling. A level alert is an alarm at the rim of the tank: by the time it rings, the water is about to spill, and our 90% heap alert was that alarm. A leak is not a high number. It is a number that keeps going up after every garbage collection. So the warning asks a different question: at the growth of the last 15 minutes, how long until we run out? It fires when the answer is under 30 minutes. The heap side, simplified:

# seconds until heap after GC reaches the old generation's max

(max by (namespace, pod) (jvm_gc_max_data_size_bytes) - max by (namespace, pod) (jvm_gc_live_data_size_bytes))

/ ((max by (namespace, pod) (jvm_gc_live_data_size_bytes) - max by (namespace, pod) (jvm_gc_live_data_size_bytes offset 15m)) / 900 > 0)

< 1800

We use a plain 15-minute difference, not predict_linear(), because heap after GC moves in steps, one per old-generation collection, and a fitted slope overshoots right after a step. If you collect with the OpenTelemetry Java agent, the same two numbers are jvm.memory.used_after_last_gc and jvm.memory.limit for the old-generation pool.

With the rule in place, the same leak gave 10.4 minutes of warning. The first version also fired on a healthy restart, which is reason 3 above. The cure: the container branch needs growth in two 15-minute windows in a row, and skips a JVM that fits its limit. After that, a healthy restart stayed quiet for 95 minutes. Every noise filter trades false alarms for a blind spot. Ours cannot warn about a leak that kills a pod within about 31 minutes of its start, and we wrote that number down.

Figure 3: The same leak in our demo with the changes in place. The trend warning (yellow) fired 26 minutes after the Helm change (red), the incident and its plan followed within a minute, and the rollback (green) landed before the container reached its 512 MiB limit. No restart.
Figure 3: The same leak in our demo with the changes in place. The trend warning (yellow) fired 26 minutes after the Helm change (red), the incident and its plan followed within a minute, and the rollback (green) landed before the container reached its 512 MiB limit. No restart.

Five leaks that look the same

A warning tells you memory will run out. It does not tell you what to do. We ran five versions of the problem, and each needed a different fix:

Figure 4: One symptom, five causes. Checked in this order, the first that fits wins.
Figure 4: One symptom, five causes. Checked in this order, the first that fits wins.
Figure
CaseWhat told it apartFirst plan that ranOutcome
A change (regression leak)growth began 8 min after Helm revision 22 set the cache cap 10000 to 0helm rollback to revision 21remediated, then 41 min quiet
A code leakgrowth at flat traffic, no change in the hour before; warning 16 min before the old generation filledkubectl rollout restart, marked mitigation only, with a heap dump and code fix as follow-upmitigated, incident left open
Loadrequest rate rose 5.4x at the step, 8.1 to 43.4 a secondhelm upgrade the limit to 768Mi, request 512Miremediated
JVM bigger than its boxMaxRAMPercentage 50 to 85: heap max plus non-heap plus 160 MiB above 512Mi; caught 11.5 min after the changehelm rollback to the revision beforeremediated about 17 min after the change
Warm-up after a restartgrowth that levels offnoneno alert for 95 min

This is why it is not just a misconfiguration. All five ended in the same OOMKilled 137, but each behaved differently on the way there: a change and a code leak climb the same way at flat traffic, load grows after a traffic step, a JVM bigger than its box dies with a calm heap curve, and a warm-up levels off. Two of them need opposite fixes. One threshold cannot sort them; it takes context that is not on the memory chart.

Two rules came out of this. Never raise the limit for a leak, and never call a restart the fix. A bigger container for a leak only buys a more expensive crash. The 768Mi plan from day one was not stupid; it was the right plan for the load case, used on the wrong incident. And “we found no change” is not a reason to stop. It is the evidence for a code leak.

It did not work the first time. The load case took five runs and the code leak six, each failure a rule we had to add: judge only the pods running now, key the load case on the traffic step, decide when memory runs out within the hour instead of asking for more data.

Fix it where it was changed

Fix it at the source, or the next deployment brings the bug right back. One failure taught us this. In an early run the right plan restored the cache cap with kubectl. The pods recovered. The Helm release still said 0. The next helm upgrade --reuse-values would bring the leak back, and the next analysis read “the Helm upgrade changed no values” and found no cause at all. The fix had erased the evidence.

So workloads managed by Helm are fixed through Helm: helm rollback to a named revision, or a values-only helm upgrade. The plan goes out as a pull request, a person approves it, and a post-flight check confirms the release holds the planned values and the trend went flat. In our latest run the warning came 26 minutes after the change. A minute later the root cause named the Helm revision and the value it changed, and the first plan was helm rollback to the revision before. The rollback landed at 08:53, two minutes before the container was projected to hit its limit, and the incident closed as remediated. It was that close only because the approval waited 15 minutes: the person was again the slowest step.

Figure 5: The root cause and the plan from that run, as the pull request showed them: one change, a Helm rollback to the revision before the leak, and two read-only checks.
Figure 5: The root cause and the plan from that run, as the pull request showed them: one change, a Helm rollback to the revision before the leak, and two read-only checks.

Test your own cluster in ten minutes

Four checks, in order, for your busiest Java service. Each takes two or three minutes. Replace shop and checkout with your namespace and Deployment.

Step 1. Do you collect the number that shows a leak?

Why: A leak shows in the heap that survives a garbage collection, not in heap used, and that gauge is often missing from the default scrape.

With Spring Boot and Micrometer:

kubectl -n shop port-forward deploy/checkout 8080:8080 &

curl -s localhost:8080/actuator/prometheus | grep -E 'jvm_gc_(live|max)_data_size_bytes'

With the OpenTelemetry Java agent, in Grafana Explore:

jvm_memory_used_after_last_gc_bytes{jvm_memory_pool_name=~".*Old Gen|Tenured Gen"}

Read the result: If this prints nothing, no alert of yours can see a leak coming.

Step 2. Who killed the last container?

Why: OOMKilled means the kernel killed it at the container limit, not Java, so your Java alerts never had a chance.

Run:

kubectl -n shop get pods -l app=checkout -o jsonpath=\

'{range .items[*]}{.metadata.name}{" "}{.status.containerStatuses[0].lastState.terminated.reason}{"\n"}{end}'

Read the result: If it says OOMKilled and you never saw a heap alert, you have the two-ceiling problem.

Step 3. Does the JVM fit its box?

Why: Heap max plus non-heap plus about 160 MiB of native memory (what we measured) must stay under the container limit.

In PromQL:

sum by (namespace, pod) (jvm_memory_max_bytes{area="heap"})

+ sum by (namespace, pod) (jvm_memory_used_bytes{area="nonheap"}) + 160 * 2^20

> on (namespace, pod) sum by (namespace, pod) (kube_pod_container_resource_limits{resource="memory"})

Read the result: Any pod this returns will be killed without any leak at all.

Step 4. What changed before it died?

Why: A leak that starts right after a deploy or a Helm value change is a regression, and the fix is to undo that change.

Run:

helm history checkout -n shop

diff <(helm get values checkout -n shop --revision 21) \

<(helm get values checkout -n shop --revision 22)

Read the result: If a change landed shortly before the deaths, start there, and fix it in Helm, not with kubectl.

Two ways to put AI on this: through the pipeline or inside the cluster

Everything above needs an AI agent to join the heap curve, the traffic and the change history within the ten minutes a slow leak gives you. There are two ways to build that agent. Most read through the observability pipeline: they see what was collected, shipped and indexed. That was fine for a person who opened a dashboard after the page, because by the time they looked, the data had landed. The alternative runs inside the cluster and reads the signals where they are made. So we timed each path, from the moment something happens in the cluster to the moment an agent can read it.

Timeline from a signal in the cluster to the moment an agent can read it, by path: an in-cluster agent reads in seconds, Prometheus and Grafana Cloud take 1 to 2.5 minutes, CloudWatch 2 to 5 minutes, CloudTrail about 5, Datadog on AWS metrics 10 to 12 minutes; plus the annual cost of scraping faster for medium and large estates.
From signal to agent: when each source becomes readable after something happens in the cluster, and what it costs to make the pipeline faster. Sources: our own run, 23 November 2026; vendor documentation and list prices.
  • An in-cluster agent watches the Kubernetes API, so it sees the OOMKill within seconds, and reads the dead container's log from the kubelet just as fast. Its own Prometheus, next to the pods, raised the alert on the kill in 2 minutes 27 seconds in our run, a minute of that a deliberate hold on the rule.
  • Prometheus with its defaults takes about 2.5 minutes, and Grafana Cloud about 1 to 2, before any hold or pending period.
  • CloudWatch Container Insights takes 2 to 3 minutes for metrics and 3 to 5 for performance log events, and CloudTrail, the record of who changed what, about 5 minutes on average.
  • Datadog on AWS metrics takes 10 to 12 minutes, plus up to a 15-minute evaluation delay. That is longer than the 10.4 minutes of warning our trend rule gave, so the warning would arrive after the pod is dead.

Buying real time multiplies the bill, and only shortens the first hop. Against a 60-second scrape, a medium estate of about 1.4 million active series pays +95% on Amazon Managed Prometheus at 30 seconds and +285% at 15, and +100% and +300% on Grafana Cloud. A large estate of about 14.6 million series would pay $4.54 million a year for Grafana Cloud metrics at a 15-second scrape, against $1.13 million at 60. The ingestion, indexing and alerting hops stay the same.

Logs lag metrics, so the evidence disagrees. Each source runs on its own clock, and CloudWatch Logs is eventually consistent. A person never noticed, because by the time they opened the logs, the logs were there. An agent reads the moment the alert fires, sees an alert the logs don't back up yet, and hedges, or decides there is nothing there. We saw the effect in our own run: the alert said the heap was leaking, the memory source the agent read showed zero, and it called the leak "unverified". Our rule now: never treat "not readable yet" as "not there."

That is why Otto reads the few signals that matter inside the cluster, where they are made, and uses the observability pipeline for history and confirmation.

The same move also cuts the bill. Central observability moves the data to the question; an in-cluster agent moves the question to the data. Once real-time decisions happen on the cluster, the three problems above shrink together: the warning arrives in seconds instead of minutes, the agent reads metrics and logs at the same moment instead of on different pipeline clocks, and you stop paying to ship every sample at real-time resolution. That does not mean moving away from your observability stack. You right-size it: real time stays at the source, history goes to the platform you already run, and cold data goes to object storage, tiered rather than deleted. For the medium estate above, about 1.4 million active series, our Right-Sizing Observability guide models a 52% cut at the conservative setting, and up to 83% when the plan follows what the root-cause analysis actually reads. Your backend gets promoted to what it does best: forensics, search and audit.

The verdict

The verdict at a glance: 0 vs 10.4 minutes of warning before the OOMKill; seconds for an in-cluster agent vs 10 to 12 minutes for AWS metrics via Datadog; +300% on the Grafana Cloud bill for a 15-second scrape; a modelled 52% to 83% cut when you right-size.

An AI SRE whose main source is observability data cannot stop a slow leak before the OOMKill. One built for remediation first can. The difference is not the model; it is what each one reads and when.

The observability-first agent inherits the limits of its source. It wakes on an alert, and for a slow leak in a container the level alerts fire after the crash or not at all. Its store holds the memory curve but not the Helm history or the pod’s start time, so it cannot tell a warm-up or more traffic from a leak. Without the cause, the one safe move left is a bigger limit: right for one of the five causes we tested, and for the regression and the code leak it only buys time until the leak fills the bigger box. That is exactly what our own agent did on day one.

The remediation-first agent reads the environment before the alert. It watches the trend on heap after GC, puts the change history and the traffic next to it, names the cause, and fixes it where the change was made: a pull request a person approves, then a check that the fix held. It is the difference between a flight recorder and an autopilot; one tells you what happened, the other flies the plane. In our latest run the warning came 26 minutes after the Helm change, and the rollback landed before the container reached its limit, with no restart.

And these failures will happen more, not less. More code is now written with AI, and developers are getting less familiar with the runtime underneath. In an Anthropic study, developers who learned a new library with AI help scored 17% lower on a comprehension quiz than those who coded by hand. Fewer people will know what heap after GC means, while more Java services run close to their limits. The answer is to use AI to protect us from these failures, the same way security teams now use AI agents, such as Google DeepMind's CodeMender, to find and fix the vulnerabilities that AI-written code brings in.

Is remediation first worth it? Yes, if you run JVMs close to their limits and deploy through Helm or GitOps. Run the ten-minute test above first; it tells you which of the two your cluster can support today.

Want to see how an autonomous SRE works on your own laptop? Our free, open-source DataAgent Lab walks you through building an agent, breaking a local Kubernetes cluster on purpose, and watching what it catches that your dashboards miss. It is on GitHub.

About the author: Nati Shalom is co-founder and CTO of DataAgent, which builds a remediation-first AI SRE for Kubernetes. He was previously founder and CTO of GigaSpaces, co-founder and CTO of Cloudify, acquired by Dell in 2023, and a Dell Fellow.

References

See remediation-first in your own stack

Install an agent and watch DataAgent map your topology. No credentials, no commitment.

Keep reading