Quick revision
The short answer
An anonymous class created inside an instance method keeps a hidden reference, the this$0 field, to the object whose method created it. The registry is long-lived, so it keeps every listener reachable, each listener keeps its Activity or Controller reachable through this$0, and the whole object graph behind each controller can never be collected. We fix it by using static nested classes that hold only what they need, by unregistering listeners when the owner closes, or by weak references where that suits the design.
What to remember
In this answer, we saw that an anonymous class created in an instance method carries a hidden this$0 reference to its creator, so a long-lived registry that holds such listeners keeps every controller and everything it references reachable. In the heap dump, the path to GC roots runs through this$0 to a class named like ReportController$1 and up to a static field. Lambdas leak the same way when they use instance members. We fixed it by unregistering when the owner closes, by using static nested listeners that hold only what they need, and in some registries by weak references.
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.
An anonymous class created inside an instance method keeps a hidden reference, the this$0 field, to the object whose method created it. The registry is long-lived, so it keeps every listener reachable, each listener keeps its Activity or Controller reachable through this$0, and the whole object graph behind each controller can never be collected. We fix it by using static nested classes that hold only what they need, by unregistering listeners when the owner closes, or by weak references where that suits the design.
Why this question is asked
Memory leaks in Java do not come from forgotten free() calls; they come from references we did not know we were keeping. This one is a classic in Android apps, desktop UIs and server-side frameworks with event registries, and it is a good interview question because the leaking reference is not visible anywhere in the source. In this answer, we will recap why an anonymous class holds its creator, reproduce the leak, read it in a heap dump, see how lambdas compare, and then go through the fixes and when each one fits.
1A Quick Recap: The Hidden Field in Every Inner Class
An anonymous class is an inner class with no name. When we create one inside an instance method, its code can use the enclosing object's fields and methods. To make that possible, javac gives the anonymous class a hidden final field, conventionally named this$0, and stores the enclosing object in it when the listener is created. The field exists whether or not the listener body uses anything from the outer object, because javac adds it for every inner class created in an instance context. Recent javac versions can omit it when the outer instance is never used, but older compiled code and many cases still have it, so we should assume it is there.
The garbage collector frees an object only when nothing reachable from a GC root, such as a static field or a live thread's stack, refers to it. A registry stored in a static field, or in a singleton service, is reachable for the life of the application.
2Reproducing the Leak
Here is a typical shape. Each controller registers a listener with a global registry when it is created and never removes it.
- GC root: EventRegistry.LISTENERSstatic field, reachable for the whole run
- CopyOnWriteArrayListholds 200 listener references
- ReportController$1 (anonymous listener)about 16 bytes, field this$0
- ReportControllerthe closed controller, should be garbage
- cache byte[5000000] and Viewthe memory we actually lose
5 layers, from the top down. Step through them one at a time.
Each level is reachable only because of the one above it.
Notice that the listener body does not even use the controller. Each listener object is tiny, perhaps 16 bytes, yet it keeps a 5 MB cache and an entire view alive. This is why the heap dump in the question shows many large objects held by small ones.
3Reading It in the Heap Dump
In a tool such as Eclipse MAT or VisualVM, we start from the dominator tree or the histogram and pick one of the large Controller objects. Then we ask for its path to GC roots, excluding weak and soft references. The path reads upward: the Controller is referenced by a field named this$0 of an object of class ReportController$1, which is referenced from an array inside a CopyOnWriteArrayList, which is referenced by the static field LISTENERS of EventRegistry.
The names give everything away. A class name with $1, $2 and so on is an anonymous class, the field this$0 is the hidden outer reference, and the static field is the long-lived owner. Counting the instances of ReportController$1 against the number of open controllers confirms it: if there are 200 listeners but only 3 open screens, 197 of them are leaks.
4Do Lambdas Have the Same Problem?
A lambda captures this only when its body uses an instance member, such as a field, an instance method or this itself. A lambda that uses nothing from the object does not hold it.
So switching to lambdas helps when the listener is truly independent, but as soon as it touches the controller's state, it holds the controller exactly like the anonymous class did. The real fix is about lifetime, not syntax.
5Fixing the Leak
The first fix is to give the listener a lifetime that matches its owner: unregister when the controller closes. This is the right fix whenever the listener exists to update that controller.
The second fix is a static nested class that keeps only the data it needs, here a short string, instead of the whole controller. If it is never unregistered it still leaks, but a few bytes per listener instead of megabytes.
The third fix is weak references, where the registry holds listeners through WeakReference and drops them once nothing else refers to them. It suits registries owned by a framework that cannot trust its users to unregister. It has a catch: if the only strong reference to a listener is the registry itself, as with a lambda passed inline, the listener can disappear while it is still wanted, so the owner must keep its own reference. In Spring applications, we usually avoid hand-made registries altogether and let the container manage listener beans.
6Follow-up questions
Why does javac add the outer reference even when the body does not use it?
Historically javac added this$0 to every inner class created in an instance context, because the class might use outer members and the field also tells the class which outer instance it belongs to. Newer javac versions can drop it from some anonymous and local classes that never use it, but we should not rely on that, and code compiled with older versions still has it.
Can a static registry be used safely at all?
Yes, with an explicit unregister path that callers cannot forget, for example registration returning an AutoCloseable handle used in try-with-resources, or tied to a lifecycle the framework controls. The leak comes from registration without a matching removal, not from the registry itself.
How do you prove the fix worked?
Run the same scenario, open and close 200 controllers, force a full GC, and take a new heap dump or check the live instance count with jcmd GC.class_histogram. The number of ReportController instances should drop to the number of open screens, and the registry size should match it.
7Conclusion
In this answer, we saw that an anonymous class created in an instance method carries a hidden this$0 reference to its creator, so a long-lived registry that holds such listeners keeps every controller and everything it references reachable. In the heap dump, the path to GC roots runs through this$0 to a class named like ReportController$1 and up to a static field. Lambdas leak the same way when they use instance members. We fixed it by unregistering when the owner closes, by using static nested listeners that hold only what they need, and in some registries by weak references.
Open with
The small listeners are anonymous classes created in instance methods, and every anonymous or inner class created in an instance context gets a hidden field, this$0, pointing to the enclosing object, whether or not the listener body uses it.
Then, if they want more
The registry is long-lived, typically a static field or a singleton, so it is reachable from a GC root, it keeps every listener alive, and each listener keeps its whole Activity or Controller alive through this$0, together with views, caches and anything else they reference. That is why a heap dump shows large objects held by tiny ones.
To confirm it, I would open the dump in Eclipse MAT, take one large controller, and show its path to GC roots excluding weak references: I expect to see this$0 in a class named like Controller$1, then the registry's collection, then a static field. Comparing the number of listeners with the number of open screens shows how many are leaks.
Lambdas are only partly better: they capture this only if they use instance members, and then they leak the same way.
The fixes are to unregister in the owner's close or destroy callback, to use a static nested listener that holds only the few values it needs, and in framework registries to hold listeners weakly, being careful that the owner keeps a strong reference.
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
- The Java Language Specification, Java SE 21, Java SE 21 API 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 heap dump shows many large Activity or Controller objects kept alive by small listener...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/de635bdb6d/