Home Software Development Avoiding reminiscence leaks with Spring Boot WebClient | bol.com

Avoiding reminiscence leaks with Spring Boot WebClient | bol.com

0
Avoiding reminiscence leaks with Spring Boot WebClient | bol.com

[ad_1]

Utilizing the dominator tree we have been capable of simply flick through the hashmap’s contents. Within the above image we opened two hashmap nodes. Right here we see a whole lot of micrometer timers tagged with “v2/merchandise/…” and a product id. Hmm, the place have we seen that earlier than?

What does WebClient must do with this?

So, it’s Spring Boot’s metrics which can be accountable for this reminiscence leak, however what does WebClienthave to do with this? To seek out that out you actually have to know what causes Spring’s metrics to retailer all these timers.

Inspecting the implementation of AutoConfiguredCompositeMeterRegistrywe see that it shops the metrics in a hashmap named meterMap. So, let’s put a well-placed breakpoint on the spot the place new entries are added and set off our suspicious name our WebClientperforms to the “v2/product/{productId}” endpoint.

We run the applying once more and … Gotcha! For every name the WebClientmakes to the “v2/product/{productId}” endpoint, we noticed Spring creating a brand new Timerfor every distinctive occasion of product identifier. Every such timer is then saved within the AutoConfiguredCompositeMeterRegistry bean. That explains why we see so many timers with tags like these:

/v2/merchandise/9200000109074941 /v2/merchandise/9200000099621587

How will you repair this reminiscence leak?

Earlier than we determine when this reminiscence leak would possibly have an effect on you, let’s first clarify how one would repair it. We’ve talked about within the introduction, that by merely not utilizing a URI builder to assemble WebClient URLs, you may keep away from this reminiscence leak. Now we’ll clarify why it really works.

After a bit of on-line analysis we got here throughout this put up (https://rieckpil.de/expose-metrics-of-spring-webclient-using-spring-boot-actuator/) of Philip Riecks, through which he explains:

“As we normally need the templated URI string like “/todos/{id}” for reporting and never a number of metrics e.g. “/todos/1337” or “/todos/42″ . The WebClient affords a number of methods to assemble the URI […], which you’ll be able to all use, besides one.”

And that methodology is utilizing the URI builder, coincidentally the one we’re utilizing:

[ad_2]