Webclient会在Docker中泄漏内存?

问题描述

我正在尝试在项目中使用Webclient,但是在进行负载测试时,我注意到docker的内存使用量永远不会下降,直到实例死亡。

@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));
  }
}

这也是我的dockerFile。

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

我错过了某个地方的行程吗?

编辑:这是一些图片

在进行负载测试之前:

Before load test

负载测试后

enter image description here

如您所见,GC正常运行,但内存并未减少。如果我让测试继续进行,它将在几分钟内杀死该实例。

我已经使用RestTemplate尝试了类似的代码,但是没有遇到任何问题,即使我长时间运行Jmeter,内存通常也不会超过400MB。您能帮忙了解发生了什么事吗?

编辑:我也尝试过不推荐使用的AsyncRestTemplate,但也没有发现任何问题。

编辑:我已经为该示例创建了存储库。请检查是否可以重现该问题。

The Hello World Backend

The Webclient Hello World(此存储库中包含JMX)

The RestTemplate Hello World

The AsyncRestTemplate Hello World

解决方法

没关系的小伙子们,我找到了答案,请参阅:

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

从本质上讲,反应堆对净值的依赖已过时。

,

我认为您的问题与RestTemplateWebClient没有任何关系。您的GC图看起来很正常。看起来好像没有泄漏,因为每当发生GC时,分配的内存似乎就可以恢复到先前的级别。

重要的是要注意,当发生垃圾回收时,您不会总是看到容器内存使用率下降。这是因为Java虚拟机不一定会在GC后将内存返回给系统。这意味着,即使在您的进程中使用了GC之后,即使部分内存未使用/释放,它也可能会在进程外部显示为“已用”。

请注意:在某些情况下,JVM 确实将内存返回给系统,这取决于各种因素,包括所使用的垃圾收集器。

从示例第二个屏幕截图的GC图形看,GC图形看起来很正常,但是似乎有很少量的堆释放回了系统(橙色区域)。

您可以尝试通过-XX:+UseG1GC JVM标志切换到G1 GC,并通过调整-XX:MinHeapFreeRatio-XX:MaxHeapFreeRatio来调整行为以更积极地将未分配的内存释放回系统,特别是通过减少-XX:MaxHeapFreeRatio值从默认的70%减小到一个较小的数字。参见here

已将-XX:MaxHeapFreeRatio降低至10%,并将-XX:MinHeapFreeRatio降低至成功地减小了堆大小,而又没有太大的性能下降;但是,结果可能会因您的应用程序而异。为这些参数尝试不同的值,直到它们尽可能的低,但仍保持可接受的性能。

有关该主题的更多信息,请参阅:

Does GC release back memory to OS?

JEP 346: Promptly Return Unused Committed Memory from G1

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...