Avoiding reminiscence leaks with Spring Boot WebClient | bol.com


Utilizing the dominator tree we have been in a position to 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 need to do with this?

So, it’s Spring Boot’s metrics which can be liable for this reminiscence leak, however what does WebClienthave to do with this? To seek out that out you actually have to grasp 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 establish when this reminiscence leak may 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’ll be able to keep away from this reminiscence leak. Now we’ll clarify why it really works.

After a bit 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, by 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 technique is utilizing the URI builder, coincidentally the one we’re utilizing:

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here