Friday, April 19, 2024
HomeSoftware DevelopmentAvoiding reminiscence leaks with Spring Boot WebClient | bol.com

Avoiding reminiscence leaks with Spring Boot WebClient | bol.com


Utilizing the dominator tree we had been capable of simply flick through the hashmap’s contents. Within the above image we opened two hashmap nodes. Right here we see plenty 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 are answerable for this reminiscence leak, however what does WebClienthave to do with this? To search 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 appliance 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 are you going to repair this reminiscence leak?

Earlier than we establish 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 possibly can keep away from this reminiscence leak. Now we are going to clarify why it really works.

After a bit on-line analysis we got here throughout this submit (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 provides 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:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments