New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Avoiding memory leaks with Spring Boot WebClient | bol.com

by admin
3 years ago
in Softwares
Women in tech | bol.com
Share on FacebookShare on Twitter


For those who’re performing internet requests with Spring Boot’s WebClient you maybe, similar to us, learn that defining the URL of your request must be achieved utilizing a URI builder (e.g. Spring 5 WebClient):

webClient .get() 
          .uri(uriBuilder -> uriBuilder.path("/v2/merchandise/{id}")
          .construct(productId))

If that’s the case, we suggest that you simply ignore what you learn (except looking hard-to-find reminiscence leaks is your passion) and use the next for establishing a URI as an alternative:

webClient .get() .uri("/v2/merchandise/{id}", productId))

On this weblog put up we’ll clarify how one can keep away from reminiscence leaks with Spring Boot WebClient and why it’s higher to keep away from the previous sample, utilizing our private expertise as motivation.

How did we uncover this reminiscence leak?

Some time again we upgraded our utility to make use of the most recent model of the Axle framework. Axle is the bol.com framework for constructing Java functions, like (REST) companies and frontend functions. It closely depends on Spring Boot and this improve additionally concerned updating from Spring Boot model 2.3.12 to model 2.4.11.

When operating our scheduled efficiency checks, the whole lot regarded wonderful. Most of our utility’s endpoints nonetheless offered response instances of beneath 5 milliseconds. Nevertheless, because the efficiency check progressed, we observed our utility’s response instances growing as much as 20 milliseconds, and after an extended operating load check over the weekend, issues acquired quite a bit worse. The response instances skyrocketed to seconds – not good.

After an extended stare down contest with our Grafana dashboards, which give insights into our utility’s CPU, thread and reminiscence utilization, this reminiscence utilization sample caught our eye:

grafana-log

This graph reveals the JVM heap dimension earlier than, throughout, and after a efficiency check that ran from 21:00 to 0:00. Through the efficiency check, the appliance created threads and objects to deal with all incoming requests. So, the capricious line displaying the reminiscence utilization throughout this era is precisely what we’d anticipate. Nevertheless, when the mud from the efficiency check settles down, we’d anticipate the reminiscence to additionally settle all the way down to the identical degree as earlier than, however it’s really larger. Does anybody else odor a reminiscence leak?

Time to name within the MAT (Eclipse Reminiscence Analyzer Device) to search out out what causes this reminiscence leak.

What triggered this reminiscence leak?

To troubleshoot this reminiscence leak we:

  • Restarted the appliance.
  • Carried out a heap dump (a snapshot of all of the objects which are in reminiscence within the JVM at a sure second).
  • Triggered a efficiency check.
  • Carried out one other heap dump as soon as the check finishes.

This allowed us to make use of MAT’s superior function to detect the leak suspects by evaluating two heap dumps taken a while aside. However we didn’t should go that far, since, the heap dump from after the check was sufficient for MAT to search out one thing suspicious:

mat report

Right here MAT tells us that one occasion of Spring Boot’s AutoConfiguredCompositeMeterRegistry occupies nearly 500MB, which is 74% of the whole used heap dimension. It additionally tells us that it has a (concurrent) hashmap that’s answerable for this. We’re nearly there!

With MAT’s dominator tree function, we are able to listing the most important objects and see what they saved alive – That sounds helpful, so let’s use it to have a peek at what’s within this humongous hashmap:

hashmap

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

What does WebClient should 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 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 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 will you 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 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, 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:

webClient .get() 
          .uri(uriBuilder -> uriBuilder.path("/v2/merchandise/{id}")
          .construct(productId))

Riecks continues in his put up that “[w]ith this resolution the WebClient doesn’t know the URI template origin, because it will get handed the ultimate URI.”

So the answer is so simple as utilizing a kind of different strategies to move within the URI, such that the WebClient WebClient will get handed the templated – and never the ultimate – URI:

webClient .get() .uri("/v2/merchandise/{id}", productId))

Certainly, after we assemble the URI like that, the reminiscence leak disappears. Additionally, the response instances are again to regular once more.

When would possibly the reminiscence leak have an effect on you? – a easy reply

Do it’s essential to fear about this reminiscence leak? Properly, let’s begin with the obvious case. In case your utility exposes its HTTP shopper metrics, and makes use of a way that takes a URI builder to set a templated URI onto a WebClient, you must undoubtedly be anxious.

You possibly can simply test in case your utility exposes http shopper metrics in two alternative ways:

  1. Inspecting the “/actuator/metrics/http.shopper.requests” endpoint of your Spring Boot utility after your utility made not less than one exterior name. A 404 means your utility doesn’t expose them.
  2. Checking if the worth of the appliance property administration.metrics.allow.http.shopper.metrics is ready to true, by which case your utility does expose them.

Nevertheless, this doesn’t imply that you simply’re protected in case you’re not exposing the HTTP shopper metrics. We’ve been passing templated URIs to the WebClient utilizing a builder for ages, and we’ve by no means uncovered our HTTP shopper metrics. But, rapidly this reminiscence leak reared its ugly head after an utility improve.

So, would possibly this reminiscence leak have an effect on you then? Simply don’t use URI builders along with your WebClient and try to be protected towards this potential reminiscence leak. That may be the easy reply. You do not take easy solutions? Honest sufficient, learn on to search out out what actually triggered this for us.

When would possibly the reminiscence leak have an effect on you? – a extra full reply

So, how did a easy utility improve trigger this reminiscence leak to rear its ugly head? Evidently, the addition of a transitive Prometheus (https://prometheus.io/) dependency – an open supply monitoring and alerting framework – triggered the reminiscence leak in our explicit case. To know why, let’s return to the scenario earlier than we added Prometheus.

Earlier than we dragged within the Prometheus library, we pushed our metrics to statsd (https://github.com/statsd/statsd) – a community daemon that listens to and aggregates utility metrics despatched over UDP or TCP. The StatsdMeterRegistry that’s a part of the Spring framework is answerable for pushing metrics to statsd. The StatsdMeterRegistry solely pushes metrics that aren’t filtered out by a MeterFilter. The administration.metrics.allow.http.shopper.metrics property is an instance of such a MeterFilter. In different phrases, if

administration.metrics.allow.http.shopper.metrics = false 

the StatsdMeterRegistry will not push any HTTP shopper metric to statsd and will not retailer these metrics in reminiscence both. Up to now, so good.

By including the transitive Prometheus dependency, we added yet one more meter registry to our utility, the PrometheusMeterRegistry. When there may be a couple of meter registry to reveal metrics to, Spring instantiates a CompositeMeterRegistry bean. This bean retains observe of all particular person meter registries, collects all metrics and forwards them to all of the delegates it holds. It’s the addition of this bean that triggered the difficulty.

The problem is that MeterFilter cases aren’t utilized to the CompositeMeterRegistry, however solely to MeterRegistry cases within the CompositeMeterRegistry (See this commit for extra data.) That explains why theAutoConfiguredCompositeMeterRegistryaccumulates all of the HTTP shopper metrics in reminiscence, even after we explicitly set administration.metrics.allow.http.shopper.metricsto false.

Nonetheless confused? No worries, simply don’t use URI builders along with your WebClient and try to be protected towards this reminiscence leak.

Conclusion

On this weblog put up we defined that this strategy of defining URLs of your request with Spring Boot’s WebClient is finest prevented:

webClient .get() 
          .uri(uriBuilder -> uriBuilder.path("/v2/merchandise/{id}")
          .construct(productId))

We confirmed that this strategy – which you might need come throughout in some on-line tutorial – is liable to reminiscence leaks. We elaborated on why these reminiscence leaks occur and that they are often prevented by defining parameterised request URLs like this:

webClient .get() .uri("/v2/merchandise/{id}", productId))



Source link

Tags: AvoidingbolcomBootLeaksMemorySpringWebClient
Previous Post

DJ Dobrel Drops Sultry House EP, “Can’t Stop Sexy” – EDM.com

Next Post

Workation & team event | Vivaldi Browser

Related Posts

How to Add Custom Style Variations to WordPress Blocks — Speckyboy
Softwares

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

by admin
June 2, 2025
Smart software replaces expensive sensors for glass wall detection with 96% accuracy
Softwares

Smart software replaces expensive sensors for glass wall detection with 96% accuracy

by admin
June 1, 2025
User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 2025
Qt bridges the language barrier gap
Softwares

Qt bridges the language barrier gap

by admin
May 28, 2025
Next Post
Workation & team event | Vivaldi Browser

Workation & team event | Vivaldi Browser

5 effective at-home exercises that don’t require any equipment – Noise

5 effective at-home exercises that don’t require any equipment – Noise

  • Trending
  • Comments
  • Latest
Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

March 1, 2024
Product Information Management Trends (PIM)

Product Information Management Trends (PIM)

February 4, 2022
Every Kathryn Hahn Film Performance, Ranked

Every Kathryn Hahn Film Performance, Ranked

December 24, 2022
Deployment Diagrams Explained in Detail, With Examples

Deployment Diagrams Explained in Detail, With Examples

August 11, 2021
6 Key Elements of Competitor Analysis That Will Help Your Business Win Out

6 Key Elements of Competitor Analysis That Will Help Your Business Win Out

May 13, 2021
The Pros and Cons of Freelance Coding [Article]

The Pros and Cons of Freelance Coding [Article]

December 5, 2023
What is Kubernetes: An Overview

An Introduction to Kubernetes | Developer.com

August 11, 2022
Bolstr bag, PowerSmart electric mower, and more – Review updates

Bolstr bag, PowerSmart electric mower, and more – Review updates

June 24, 2023
May 30-June 1 Box Office Recap – ‘Lilo & Stitch’ crosses $600M worldwide, while ‘Mission: Impossible – The Final Reckoning’ crosses $350M worldwide. ‘Karate Kid: Legends’ and ‘Bring Her Back’ have solid debuts, while ‘The Phoenician Scheme’ opens with the best per-theater average ($93K) of the year.

May 30-June 1 Box Office Recap – ‘Lilo & Stitch’ crosses $600M worldwide, while ‘Mission: Impossible – The Final Reckoning’ crosses $350M worldwide. ‘Karate Kid: Legends’ and ‘Bring Her Back’ have solid debuts, while ‘The Phoenician Scheme’ opens with the best per-theater average ($93K) of the year.

June 3, 2025
Samsung may incorporate Perplexity’s AI tech in its phones

Samsung may incorporate Perplexity’s AI tech in its phones

June 2, 2025
Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello

Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello

June 2, 2025
Lady Isabella Hervey on bouncing back from her unhappy marriage and her new life on the Algarve

Lady Isabella Hervey on bouncing back from her unhappy marriage and her new life on the Algarve

June 2, 2025
19 Sustainable Clothing Brands For Stylish Men In 2025

19 Sustainable Clothing Brands For Stylish Men In 2025

June 2, 2025
How to Add Custom Style Variations to WordPress Blocks — Speckyboy

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

June 2, 2025
Instagram Creators With Over 100K Followers Will Get Access to Additional Comment Filters

Instagram Creators With Over 100K Followers Will Get Access to Additional Comment Filters

June 2, 2025
Fantastic Four Star Vanessa Kirby Is Pregnant!

Fantastic Four Star Vanessa Kirby Is Pregnant!

June 2, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • May 30-June 1 Box Office Recap – ‘Lilo & Stitch’ crosses $600M worldwide, while ‘Mission: Impossible – The Final Reckoning’ crosses $350M worldwide. ‘Karate Kid: Legends’ and ‘Bring Her Back’ have solid debuts, while ‘The Phoenician Scheme’ opens with the best per-theater average ($93K) of the year.
  • Samsung may incorporate Perplexity’s AI tech in its phones
  • Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

free 100 upon registration casino