Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After a release, heap use grows steadily and the heap dump shows millions of Micrometer meter objects. The release added a timer tagged with the request path and the user id. Explain the leak and fix.

Micrometer creates and keeps a separate meter for every unique combination of a meter's name and tag values, and the registry holds them forever. Tagging the timer with user ids and raw request paths makes that combination space unbounded, so the meter count, and the heap, grow with every new user and every new id in a URL, and the metrics backend is overloaded too. The fix is to use bounded tags, such as the route template instead of the raw path, drop the user id, add meter filters that cap or deny high-cardinality tags, and review metric tags in code review.

Why this question is asked

High-cardinality metrics are one of the most common causes of memory leaks in modern Spring services, and also one of the most surprising, because the code that causes them looks perfectly reasonable: one line that records a timer with two helpful tags. Interviewers ask this question because it tests whether we understand how a metrics library stores data, and because the same mistake costs money in the monitoring system as well as memory in the service. In this answer, we will recap how Micrometer stores meters, reproduce the leak, see why it also hurts the backend, and then fix it with bounded tags, meter filters and review rules.

1A Quick Recap of Meters and Tags

Micrometer is the metrics library Spring Boot uses. A meter is one measured thing, such as a counter, a gauge or a timer. Each meter has a name, such as http.client.time, and a set of tags, key-value pairs that describe it, such as method=GET or status=200. The name plus the exact tag values form the meter's identity, called its id.

The MeterRegistry holds every meter that has been registered, in a map from id to meter. When code asks for a timer with a given name and tags, the registry looks up that id; if it exists, it returns the same timer, and if not, it creates a new one and keeps it. A timer is not a tiny object: it holds counts, totals, a maximum with a decaying window and, if percentiles or histograms are enabled, buckets for them. So each meter costs from hundreds of bytes to several kilobytes.

2Reproducing the Leak

Here is the kind of code the release added.

Java
public Order getOrder(HttpServletRequest request, String userId) {
return Timer.builder("orders.request")
.tag("path", request.getRequestURI()) // "/orders/88231", "/orders/88232", ...
.tag("user", userId) // "u-1001", "u-1002", ... every user
.register(registry) // new id -> new Timer kept forever
.record(() -> orderService.load(request));
}
registry.find("orders.request").timers().size(); // 1 after the first request
// after a day with 400,000 users and 2 million distinct order ids:
registry.find("orders.request").timers().size(); // millions
Meters created for the same timer
Requestpath taguser tagResult
1/orders/88231u-1001new Timer #1
2/orders/88231u-1001same Timer #1
3/orders/88232u-1001new Timer #2
4/orders/88232u-1002new Timer #3
after a day2 million values400,000 valuesmillions of Timers
Every new tag combination is a new meter that the registry never forgets.

The tag values come from unbounded sets. Every new user id creates a new combination, and the raw path contains the order id, so every new order creates one too. The number of possible ids is the number of users times the number of paths. Since the registry keeps every meter it ever created, the count only goes up, and the heap dump shows millions of Timer objects and their tag lists. Nothing is wrong with Micrometer; it is doing exactly what it was told.

3Why It Also Hurts the Metrics Backend

Each meter becomes one or more time series in the monitoring system. A timer exported to Prometheus, for example, produces a count, a sum and a max series, and more with histograms. Millions of meters mean millions of series, each of which the backend has to index and store. That makes scrapes slow and large, can exceed the backend's series limits, and in hosted monitoring products is often billed per series, so the bill grows with the user base.

Queries suffer too. A dashboard that sums the timer over all paths must read millions of series. So even a service with plenty of memory would need this fixed.

4Fixing the Tags

The fix is to use only tags whose values come from a small, known set. For the path, we use the route template, the pattern the controller is mapped to, such as /orders/{id}, which has as many values as we have endpoints. Spring's own http.server.requests metric already does this with its uri tag. For the user id, we drop it. Per-user information belongs in logs or traces, where each record is stored once rather than becoming a permanent series.

Java
public Order getOrder(HttpServletRequest request, String userId) {
String route = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
return Timer.builder("orders.request")
.tag("route", route != null ? route : "UNKNOWN") // "/orders/{id}": a few dozen values
.tag("outcome", "SUCCESS") // bounded set of outcomes
.register(registry)
.record(() -> orderService.load(request));
}
registry.find("orders.request").timers().size(); // equals the number of routes, e.g. 12

Notice the fallback for a missing route. Requests that match no handler, such as scans for random URLs, would otherwise create a new value per URL, which is exactly the problem we are fixing. In many cases the simplest fix is to delete the custom timer entirely, because http.server.requests already measures every endpoint with bounded tags.

5Guard Rails: Meter Filters and Code Review

Fixing one timer does not stop the next developer from adding a similar tag. Meter filters, rules applied by the registry before a meter is created, give us a safety net. We can deny a tag key everywhere, or cap the number of distinct values for a tag, after which new meters are denied instead of created.

Java
@Bean
MeterRegistryCustomizer<MeterRegistry> cardinalityGuards() {
return registry -> registry.config()
.meterFilter(MeterFilter.ignoreTags("user", "userId", "sessionId")) // never a tag
.meterFilter(MeterFilter.maximumAllowableTags(
"orders.request", "route", 100, MeterFilter.deny())); // cap at 100 values
}

With these filters, a user tag is silently removed, and if the route tag ever exceeds 100 distinct values, new meters for it are denied, so the leak is capped. We pair this with an alert on the number of meters or series per service, and a code review rule: every new tag needs an answer to the question of how many values it can have, and anything from user input, ids or free text is rejected. Spring Boot also has its own property to cap URI tags for its HTTP metrics; check your version for the exact name.

6Follow-up questions

How do we remove the millions of meters already in memory?

Restarting the service clears the registry, which is the practical answer once the fixed code is deployed. Meters can also be removed with registry.remove(meter), but finding and removing millions of them at runtime is rarely worth it.

What if the business really wants latency per customer?

For a small, fixed set, such as ten enterprise customers or a customer tier, a tag is fine because the values are bounded. For all customers, we use traces or logs with the customer id, and compute per-customer statistics in the log or tracing system, which is built for high-cardinality data.

How do we find which meter is the problem?

In the heap dump, the dominator tree shows the registry's meter map as the largest object, and inspecting a few meter ids shows the name and tags. On a live service, the /actuator/metrics endpoint lists meter names, and counting meters per name in the registry or series per metric name in the backend points to the culprit.

7Conclusion

In this answer, we saw that Micrometer keeps one meter for each unique combination of name and tag values, and the registry never forgets a meter. Tagging a timer with user ids and raw paths makes the combinations unbounded, so meters pile up in the heap and become millions of time series in the backend. The fix is bounded tags, such as the route template, no user ids, meter filters that ignore dangerous tags and cap the number of values, and a code review rule that every tag must have a known, small number of values.

What to say in the interviewAbout 80 seconds aloud

Open with

In Micrometer, a meter's identity is its name plus its exact tag values, and the registry keeps every meter it creates in a map, forever. So each unique combination of path and user id creates a new Timer with its own counters and max window.

Then, if they want more

User ids are unbounded, and a raw path like /orders/88231 contains an id, so the number of meters grows with every user and every order, which is exactly the millions of meter objects in the heap dump. It also overloads the metrics backend, because each meter becomes several time series, which slows scrapes and queries and costs money per series.

The fix is to use only bounded tags: the route template, like /orders/{id}, instead of the raw URI, with a fallback value when there is no match, and no user id at all; per-user detail belongs in logs and traces. Often the custom timer is not needed, since http.server.requests already uses the uri template.

To prevent it coming back, I would add meter filters, ignoreTags for user and session ids, and maximumAllowableTags to cap values and deny new meters beyond the cap, plus an alert on meter count, and a review rule that every new tag must state how many values it can have.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After a release, heap use grows steadily and the heap dump shows millions of Micrometer...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/7455f8972e/