Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webclient Leaks Memory In Docker?

I'm trying to use Webclient in my project, but when I load test, I'm noticing the docker memory usage never goes down until the instance dies.

@Component
public class Controller {

  //This is an endpoint to another simple api
  //I use my local Ip instead of localhost in the container
  private static final String ENDPOINT = "http://localhost:9090/";

  private WebClient client;
  
  public Controller(WebClient.Builder client) {
    super();
    this.client = client.build();
  }

  @Bean
  public RouterFunction<ServerResponse> router() {

    return RouterFunctions.route(GET("helloworld"), this::handle);
  }

  Mono<ServerResponse> handle(ServerRequest request) {

    Mono<String> helloMono =
        client.get().uri(ENDPOINT + "/hello").retrieve().bodyToMono(String.class);

    Mono<String> worldMono =
        client.get().uri(ENDPOINT + "/world").retrieve().bodyToMono(String.class);

    return Mono.zip(helloMono, worldMono, (h, w) -> h + w)
        .flatMap(s -> ServerResponse.ok().bodyValue(s));
  }
}

Here's my dockerFile as well.

FROM openjdk:8

ENV SERVICE_NAME reactive-hello-world

ADD target/reactive-hello-world-*.jar $APP_HOME/reactive-hello-world.jar

RUN mkdir /opt/reactor-netty/


EXPOSE 9010

CMD java \
    -Dcom.sun.management.jmxremote=true \
    -Dcom.sun.management.jmxremote.local.only=false \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Djava.rmi.server.hostname=localhost \
    -Dcom.sun.management.jmxremote.port=9010 \
    -Dcom.sun.management.jmxremote.rmi.port=9010 \ 
    -Xmx190M \
    -jar reactive-hello-world.jar

EXPOSE 8080

Have I missed a step somewhere?

Edit: Here's some images

Before Load Test: Before load test

After Load Test

enter image description here

As you can see, the GC is happening correctly but the memory hasn't decreased. If I let the test continue it kills the instance in a couple minutes.

I've tried similar code using RestTemplate and I'm not experiencing any issues, the memory doesn’t usually exceed 400MB even when I run the Jmeter for an extended time. Can you help understand what's happening?

Edit: I've also tried the deprecated AsyncRestTemplate and I'm not seeing a problem with that either.

Edit: I have created the repos for this example. Please check if you can reproduce the issue.

The Hello World Backend

The Webclient Hello World(JMX is inside this repo)

The RestTemplate Hello World

The AsyncRestTemplate Hello World

like image 368
Jojo Avatar asked Aug 03 '26 20:08

Jojo


1 Answers

Nevermind lads, I was able to find the answer, see:

https://github.com/reactor/reactor-netty/issues/1304

Essentially, the reactor netty dependency was outdated.

like image 170
Jojo Avatar answered Aug 05 '26 13:08

Jojo