question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ByteBuffer memory leak in retry mechanism

See original GitHub issue

What version of gRPC-Java are you using?

Bug detected on latest code from master branch (at the time of this post), and also 1.45 (the version used in production where the leak was noticed).

What is your environment?

Tested on macos (12.4) and centos7 with jdk11. In production where the leak was originally detected, it’s debian with jre17

What did you expect to see?

These bytebuffer should be getting released.

What did you see instead?

Bytebuffers getting allocated and not released when a retry is triggered, it’s causing OOMs in production (and in my local code reproduction).

Steps to reproduce the bug

I modified the hello-world example to showcase this bug.

git clone git@github.com:grpc/grpc-java.git
cd grpc-java/examples

then in the src/main/java/io/grpc/examples/helloworld dir you’ll see the client and server. Replace the client with my client: https://gist.github.com/davidnadeau/055f88cec499a6afb502c9e5f231a271 (I just change the stub to be async and use a deadline, then call the greet method in a loop). Then you can run from the examples dir:

./gradlew installDist
Then in their own tabs:
./build/install/examples/bin/hello-world-server
./build/install/examples/bin/hello-world-client

with more verbose leak logging
HELLO_WORLD_CLIENT_OPTS="-Dio.grpc.netty.shaded.io.netty.leakDetection.level=paranoid " ./build/install/examples/bin/hello-world-client

It’s definitely related to the retry mechanism, since adding .disableRetry() to the client channel makes the leak go away. I believe the deadline is causing the retry to trigger (you can see in the logs that the deadline is triggering).

Full leak log here: https://gist.github.com/davidnadeau/4da26f072482fb58b19f4bd0379f35c7

There is a noticeable latency increase when disabling retries which prevents it from being a viable solution for my case.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:7
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
sheilahucommented, Aug 8, 2022

👋 Hi! Thanks for porting back to 1.47.x. I wonder if the actual binary has been released on maven yet?

0reactions
davidnadeaucommented, Sep 15, 2022

Thanks a lot for taking the time to fix this 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing Java's ByteBuffer native memory "leak" (evanjones.ca)
TL;DR: The Java NIO API caches a maximum-sized direct ByteBuffer for each thread, which looks like a native memory leak if you read...
Read more >
java - Will ByteBuffer.wrap(byte[]) cause memory leak for long ...
The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be...
Read more >
DataStax Java Driver - Changelog
[bug] JAVA-2204: Avoid memory leak when client holds onto a stale TableMetadata ... [bug] JAVA-1944: Surface Read and WriteFailureException to RetryPolicy.
Read more >
LeakCanary: Detect all memory leaks! | Square Corner Blog
A memory leak detection library for Android and Java. ... Catch OutOfMemoryError, trigger the GC and retry a few times (inspired from ...
Read more >
Beyond ByteBuffers by Brian Goetz - YouTube
Accessing foreign (e.g. off-heap) memory from a Java application can be useful for a number of reasons, from avoiding the costs associated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found