DEV Community

kkkevin
kkkevin

Posted on Originally published at opshubs.space

Netty Direct Memory Leak in Spring Cloud Gateway: A Post-Mortem

During our recent high-concurrency production launch, our API Gateway cluster encountered a critical infrastructure bottleneck. After running smoothly under a simulated 10,000 TPS workload during the initial green-light testing phases, the production instances began dropping connections and triggering silent JVM crashes after roughly 36 hours of continuous uptime. 

Surprisingly, standard APM dashboards showed that Heap Memory utilization (-Xmx) was perfectly stable, hovering around a healthy 45%. However, the underlying container environment kept reporting an OS-level Out-Of-Memory (OOM) killer event.

java.lang.OutOfMemoryError: Direct buffer memory.
at io.netty.buffer.PlatformDependent.incrementMemorySize(PlatformDependent.java:…)
Enter fullscreen mode Exit fullscreen mode

The Root Cause: Asynchronous Reactive Leaks

Upon conducting a thread-dump analysis and taking native memory snapshots via jcmd, we tracked the culprit down to a custom global logging filter implemented inside our reactive Spring Cloud Gateway framework.

In a non-blocking reactive stream (built on Project Reactor and Netty), response bodies are handled as continuous chunks of DataBuffer instances. Our logging filter was intercepting the downstream response payload to log specific financial voucher transaction metadata. However, instead of safely releasing the asynchronous data slice, the thread pipeline occasionally bypassed the final step:

// The antipattern causing the leak:
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
    DataBuffer buffer = ... // Read payload metadata
    // CRITICAL: missing DataBufferUtils.release(buffer);
}));
Enter fullscreen mode Exit fullscreen mode

Because Netty allocates pooled direct memory buffers outside the standard JVM garbage collection (GC) boundaries to achieve zero-copy high-throughput network streaming, these unreleased chunk references accumulated continuously in the native OS ram pool until total depletion occurred.

Resolution Blueprint

To remediate this production threat without impacting performance thresholds, we implemented the following multi-tiered fixes:

  • Explicit DataBuffer Lifecycle Control: Rewrote the interceptor layer to meticulously leverage DataBufferUtils.release(buffer) inside the continuous doOnDiscard() and error handling reactive operators, ensuring zero orphan buffers.

  • Leaked Buffer Tracking Activation: Injected the -Dio.netty.leakDetection.level=PARANOID JVM routing argument into our container launch scripts for the next rolling deployment update. This forces Netty to explicitly track buffer allocation root traces at the cost of slight CPU overhead, serving as our production early-warning defense.

  • OS-Level Containment: Adjusted the container cgroup limits, providing a 25% safety buffer for native non-heap memory allocations to handle unpredictable network socket bursts.

By standardizing these reactive pipeline rules, our gateway cluster has achieved total stability with strict native memory containment. Never take data buffers for granted in a high-concurrency microservices perimeter.

Sizing your own gateway? Our JVM Container Memory Calculator estimates heap, direct buffers and other off-heap memory against your container limit, and shows the worst case when MaxDirectMemorySize is left unset.

Related reading

Originally published at OpsHubs. Found it useful? Buy us a coffee ☕.

Top comments (0)