Quick revision
The short answer
Micrometer gauges hold only a weak reference to the object they observe. registry.gauge("queue.size", new ArrayList<>(queue), List::size) observes a brand new list that nothing else references, so the garbage collector soon removes it and the gauge reports NaN; even before that, it reports the size of a frozen copy, not of the live queue. The fix is to observe an object that lives as long as the gauge, such as the queue itself or a field, or to build the gauge with Gauge.builder(...).strongReference(true) when that is appropriate. Gauges sample a current value, so for counting events we use counters instead.
What to remember
In this answer, we saw that Micrometer gauges read their value from an object at scrape time and hold that object only through a weak reference. Registering a gauge on new ArrayList<>(queue) observes a frozen copy that nothing else references, so it never changes and turns NaN after a garbage collection. Observing the queue itself, keeping the object in a field, or using strongReference(true) fixes it, and for counting events we use a counter instead of a gauge.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Micrometer gauges hold only a weak reference to the object they observe. registry.gauge("queue.size", new ArrayList<>(queue), List::size) observes a brand new list that nothing else references, so the garbage collector soon removes it and the gauge reports NaN; even before that, it reports the size of a frozen copy, not of the live queue. The fix is to observe an object that lives as long as the gauge, such as the queue itself or a field, or to build the gauge with Gauge.builder(...).strongReference(true) when that is appropriate. Gauges sample a current value, so for counting events we use counters instead.
Why this question is asked
This bug is a good example of an API that is designed to avoid one problem, memory leaks, and causes another when we do not know about the design. The gauge works perfectly in a quick test and then shows NaN in production after the first garbage collection. Interviewers ask about it to see whether we understand how gauges read their value and what a weak reference is. In this answer, we will recap how gauges work, reproduce the problem, explain both things wrong with the snippet, fix it in two ways, and finish with when a gauge is the wrong meter altogether.
1A Quick Recap: How a Gauge Gets Its Value
A Counter or a Timer receives values when we push them: we call increment() or record(). A Gauge is different. We give it an object and a function, and whenever the metrics backend scrapes or publishes, the gauge calls the function on the object to read the current value. For a queue, the object is the queue and the function is its size method.
So the gauge has to keep hold of the object between scrapes. Micrometer chose to hold it through a weak reference, a reference that does not keep the object alive. If nothing else in the program references the object, the garbage collector is free to reclaim it, and the weak reference then returns null. The reason for this design is to avoid memory leaks: registering a gauge on some short-lived object should not keep that object in memory forever just because a metric points to it.
2Reproducing the Problem
Here is the code from the question in a small component.
| Moment | Real queue size | Gauge on the copy | Gauge on the queue |
|---|---|---|---|
| Startup | 0 | 0 | 0 |
| Jobs arrive | 500 | 0 (frozen copy) | 500 |
| After a GC | 480 | NaN (copy collected) | 480 |
| Queue drains | 0 | NaN | 0 |
Two things are wrong here. First, new ArrayList<>(queue) makes a copy of the queue's contents at construction time. Even while the copy exists, the gauge reports the size of that frozen copy, so it never changes when jobs are added to or taken from the real queue: it stops updating. Second, the copy is referenced only by the gauge's weak reference. As soon as the constructor returns, nothing else holds it, so at the next garbage collection it is reclaimed, the gauge's function has nothing to call, and Micrometer reports NaN, not a number.
3Fix 1: Observe the Object That Lives as Long as the Gauge
The natural fix is to observe the real queue. The queue is a Spring bean or a field of one, so it is strongly referenced by the application for as long as it runs, and the weak reference stays valid.
Notice that registry.gauge returns the object it observes, which allows a common and correct pattern for values we maintain ourselves: AtomicInteger active = registry.gauge("jobs.active", new AtomicInteger(0)); kept in a field. Because the field holds the AtomicInteger strongly, the gauge stays alive, and we update it with active.incrementAndGet(). The broken version of that pattern is calling registry.gauge with a new AtomicInteger and ignoring the return value, which fails exactly like our list.
4Fix 2: Ask for a Strong Reference
Sometimes the object really has no other owner, for example a small holder created only for the metric. Then we can tell Micrometer to keep it alive with strongReference(true) on the builder.
This is appropriate when the gauge should live for the whole application, which is the usual case for metrics registered at startup. The trade-off is the one Micrometer's default avoids: if gauges with strong references are created repeatedly for short-lived objects, those objects can never be collected, so memory leaks. For our queue, observing the queue itself is better than holding a copy strongly, because a strongly held copy would still show a frozen size.
5When a Gauge Is the Wrong Meter
A gauge answers the question what is the value right now: the size of a queue, the number of active connections, the free disk space. It is only sampled at scrape time, perhaps every 15 or 30 seconds, so anything that happens between samples is invisible.
If what we want is how many jobs were added or processed, that is a count of events, and a Counter is the right meter: we increment it on every event, the backend computes rates, and nothing is missed between scrapes. A common mistake is a gauge on a field that holds a running total, which loses its value on restart and cannot produce reliable rates. So for this queue we might use both: a gauge for its current size and a counter for jobs enqueued and processed.
- Counting events?YesUse a CounterNoIt is a current value
- Use a Gaugesampled at scrape time
- Object referenced elsewhere for the app's lifetime?YesObserve it directlyNoUse strongReference(true) or keep it in a field
- Never observe a temporary copyit freezes and then becomes NaN
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why does the gauge work in tests but fail in production?
In a short test, the garbage collector may never run, so the weak reference stays valid and the gauge reads the copy. In production, garbage collection runs within seconds or minutes, the copy disappears, and the gauge turns NaN. Calling System.gc() in a test can reproduce it, though the JVM does not guarantee a collection.
Does the gauge disappear from the registry when its object is collected?
No. The meter stays registered and reports NaN, which is why the symptom is a NaN series rather than a missing metric. To remove a gauge on purpose, we call registry.remove with the meter.
Is TimeGauge or a FunctionCounter related to this?
Yes. TimeGauge and FunctionCounter also read their value from an object through a function, and they also hold weak references by default, so the same rule applies: observe an object that outlives the meter.
7Conclusion
In this answer, we saw that Micrometer gauges read their value from an object at scrape time and hold that object only through a weak reference. Registering a gauge on new ArrayList<>(queue) observes a frozen copy that nothing else references, so it never changes and turns NaN after a garbage collection. Observing the queue itself, keeping the object in a field, or using strongReference(true) fixes it, and for counting events we use a counter instead of a gauge.
Open with
A gauge does not receive values; at each scrape it calls the function on the object I gave it, here List size on the list. Micrometer holds that object only through a weak reference, so the gauge does not keep it alive.
Then, if they want more
That is deliberate, to avoid memory leaks from gauges on short-lived objects. The snippet has two problems. First, new ArrayList of queue is a copy made once, so even while it exists the gauge reports the size of a frozen snapshot, not the live queue, which is why it stops updating.
Second, nothing else references that copy, so after the next garbage collection it is gone and the gauge reports NaN. It usually works in a short test because no collection happens.
The fix is to observe something that lives as long as the gauge, which here is the queue itself: Gauge dot builder queue dot size, queue, BlockingQueue size, register, or registry dot gaugeCollectionSize. For values I maintain myself, I keep the AtomicInteger that registry dot gauge returns in a field.
If the object truly has no other owner, the builder's strongReference true makes the gauge hold it, accepting that it will never be collected, which is fine for meters that live for the whole application.
And I would point out that gauges are for current values sampled at scrape time; if I want to know how many jobs were enqueued or processed, that is a counter, which does not miss events between scrapes.
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.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
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 "A Gauge registered with registry.gauge("queue.size", new ArrayList<>(queue), List::size)...": https://scrivio-interview-library.netlify.app/spring-boot/observability/24267f81d3/