Imagine a Java 21 service that counts completed jobs by tenant. After a configuration update, some map entries disappear from lookups. Under load, totals come up short. An hour later the service uses more memory than it did at startup. These are three different problems until you have evidence otherwise.

Start with the keys

1. Can an entry still exist if HashMap cannot find its key?

Yes. If a key changes in a way that affects equality while it is in the map, the map contract does not specify what happens. A changed hash can direct lookup to the wrong bucket even when iteration still shows the entry. For this job counter, key by a stable tenant ID rather than an editable tenant object. A record is not a deep immutability guarantee: a record component can itself refer to a mutable collection.

2. Which collection would you pick for the report?

If the report only needs key lookup and no ordering guarantee, start with a map. If it needs insertion order, use a LinkedHashMap in a single-threaded reporting step. If it needs sorting by tenant name, sort a snapshot rather than paying to maintain a sorted order on every write. Ask whether the report needs a coherent point-in-time view. A copy made while a concurrent map is changing does not, by itself, guarantee one.

Separate visibility from atomicity

3. Why is a volatile counter still losing updates?

count++ reads, adds, and writes. Two threads can read the same value and write the same successor even if the variable is volatile. Use an atomic increment for one counter, or put a compound invariant under one lock. For increments by tenant, the following map operation makes that individual update atomic; it does not make an update across two keys atomic.

var completed = new ConcurrentHashMap<String, Long>();
completed.merge(tenantId, 1L, Long::sum);

4. Do virtual threads remove the need for limits?

No. Virtual threads in Java 21 make many concurrent blocking tasks practical, but do not increase CPU capacity or the number of database connections. Limit use of the scarce downstream resource, for example through a connection pool, and give waiting work a deadline. Compare a CPU-bound reporting transform with a task waiting on a database response: the latter has waiting time to overlap, while the former still needs CPU time.

Test the memory hypothesis

5. Does higher process memory prove a heap leak?

No. Look first at heap use after comparable garbage collection cycles and at live-object trends under a comparable workload. A growing tenant map may retain old entries indefinitely, but process memory also includes memory outside the Java heap. If heap occupancy stabilizes while resident memory grows, do not stop at the heap graph. Compare observations over time and inspect the objects retaining memory before changing heap size or garbage collector settings.

Practice explaining the next measurement

  • Write a key whose hash depends on an editable field. Explain why changing it violates the contract before running a test.
  • Run concurrent increments, then change the requirement to update two values together. State exactly where the atomic boundary must move.
  • Sketch two memory graphs, one with a rising post-collection baseline and another with steady heap occupancy. Name the first evidence you would collect in each case.

For more on deciding how much detail to prepare, see choosing interview topic depth. Then use Mentaro's free practice demo to rehearse answering a sample card before revealing the explanation.

Sources