question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potential memory leak in MetricsClientHttpRequestInterceptor

See original GitHub issue

Looks like there’s a bug in this code in org.springframework.boot.actuate.metrics.web.client.MetricsClientHttpRequestInterceptor class, lines 97 - 99:

    if (urlTemplate.get().isEmpty()) {
        urlTemplate.remove();
    }

I believe the if condition should have a not operator in front of it, meaning item(s) should be removed if the list held by urlTemplate thread-local is not empty.

This came up when looking at a memory leak in code, which is likely an edge case and also uses RestTemplate and UriTemplateHandler in a way some could characterize as misuse. The problem can be reproduced with roughly this kind of logic:

    String template = "https://example.org/api/users/{userId}";
    for (int i : IntStream.range(0, 10000).toArray()) {
        logger.debug("Request to {}", restTemplate.getUriTemplateHandler().expand(template, UUID.randomUUID()));
    }

New items are added to the list, but never removed due to the missing not operator. The accumulated list is visible in heap dump after that loop has finished.

This doesn’t seem to be an issue for example in handling of incoming requests. It also looks like there’s no problem in how RestTemplate uses the mechanism internally. Long-lived thread with manual UriTemplateHandler.expand() usage with metrics actuator enabled is the key.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Jun 21, 2022

@sinsuren Spring Boot 2.2.x is missing fixes for bugs in this area such as https://github.com/spring-projects/spring-boot/issues/26915 that’s linked to above. Its open source support also ended in October 2020. You should upgrade to Spring Boot 2.6.x or 2.7.x as soon as possible.

0reactions
bclozelcommented, Aug 25, 2022

I don’t think we’re going to be able to close this issue in the 2.x line without breaking other use cases. This all comes from the fact that RestTemplate is instrumented through a contract that’s not meant for this. We’re going to address this in Spring Framework 6 & Spring Boot 3 by instrumenting the HTTP clients directly with micrometer Observation.

I’m closing in favor of spring-projects/spring-framework#28341

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoiding memory leaks with Spring Boot WebClient | bol.com
In this blog post we'll explain how to avoid memory leaks with Spring ... and you should be protected against this potential memory...
Read more >
Understanding Memory Leaks in Java - Baeldung
Memory leaks are a genuine problem in Java. In this tutorial, we'll learn what the potential causes of memory leaks are, how to...
Read more >
Hunting Java Memory Leaks - Toptal
In this post, I'll explain how and why memory leaks occur in Java and outline an approach for detecting such leaks with the...
Read more >
3 Troubleshoot Memory Leaks - Java - Oracle Help Center
This chapter provides some suggestions for diagnosing problems involving possible memory leaks. If your application's execution time becomes longer and longer, ...
Read more >
Spring + MongoDB: potential memory leak messages
We need to clean them up to prevent memory leaks. * * @return number of Thread local variables */ private int immolate() {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found