Quick revision
The short answer
javac compiles each lambda body into a private synthetic method in the enclosing class, and at the lambda's position it emits an invokedynamic instruction whose bootstrap method, LambdaMetafactory, creates the functional interface object at run time. A non-capturing lambda typically gets one shared instance per call site, so evaluating it again allocates nothing. A lambda that captures locals gets a new instance each time, with the captured values stored as fields. A lambda that uses an instance field or method captures this, which can keep the enclosing object alive as long as the lambda lives.
What to remember
In this answer, we saw that javac turns each lambda body into a private synthetic method, static unless it uses this, and emits an invokedynamic instruction. On first execution, LambdaMetafactory generates a hidden class implementing the interface and links the call site to a factory. Non-capturing lambdas typically return one shared instance with no allocation, lambdas that capture locals allocate a new instance holding the values as fields, and lambdas that use instance members capture this, which can keep the enclosing object alive as long as the lambda, as in the listener leak.
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.
javac compiles each lambda body into a private synthetic method in the enclosing class, and at the lambda's position it emits an invokedynamic instruction whose bootstrap method, LambdaMetafactory, creates the functional interface object at run time. A non-capturing lambda typically gets one shared instance per call site, so evaluating it again allocates nothing. A lambda that captures locals gets a new instance each time, with the captured values stored as fields. A lambda that uses an instance field or method captures this, which can keep the enclosing object alive as long as the lambda lives.
Why this question is asked
Most of the time we can treat lambdas as magic: we write one and it works. But questions about allocation, memory leaks and stack traces all lead back to how lambdas are compiled and created. Interviewers ask this internals question to see whether we can reason about what a lambda costs and what it holds on to. In this answer, we will look at the synthetic method javac generates, follow the invokedynamic call through LambdaMetafactory, compare non-capturing, local-capturing and this-capturing lambdas with javap output and an identity check, and finish with the listener memory leak that capturing this can cause.
1The Body Becomes a Private Method
When javac compiles a lambda, it moves the body into a new private method of the enclosing class, with a generated name such as lambda$main$0 (the method the lambda appeared in and a counter). Captured local values become leading parameters of that method. If the lambda does not use this, the method is static; if it does, the method is an instance method.
Notice that there is no class file per lambda, unlike anonymous inner classes, which each produce a Demo$1.class. The body lives in Demo itself. That is also why lambda frames in stack traces carry names like lambda$capturingLocal$1.
2Invokedynamic and LambdaMetafactory
At the place where the lambda appears, javac emits an invokedynamic instruction. invokedynamic, added in Java 7, is an instruction whose target is decided at run time by a bootstrap method, the first time it executes. For lambdas, the bootstrap method is LambdaMetafactory.metafactory.
7 messages in order. Play them, or step through them one at a time.
The bootstrap runs once per call site; later evaluations only create or return an instance.
On the first execution, the JVM calls the bootstrap method with a description of the lambda: the interface to implement (Runnable), its method (run), and a handle to the synthetic method. LambdaMetafactory then generates a small class at run time, a hidden class since Java 15, that implements Runnable, has one field per captured value, and whose run() calls lambda$capturingLocal$1 with those fields. The call site is then linked to a factory that creates instances of that class. Later executions skip the bootstrap entirely and just call the factory.
The descriptor tells us the capture story. The captured values are the factory's arguments: none for a non-capturing lambda, a String for capturingLocal, and a Demo, which is this, for capturingThis.
3Comparing the Three Kinds of Lambda
The capture difference shows up directly in allocation and object identity. Let us evaluate each lambda twice and compare the results with ==.
| Kind | Synthetic method | Each evaluation | Holds a reference to |
|---|---|---|---|
| Non-capturing | private static | returns one shared instance | nothing |
| Captures locals | private static, values as params | allocates a new instance | copies of the captured values |
| Captures this | private instance method | allocates a new instance | the enclosing object |
A non-capturing lambda has no state, so the factory for its call site returns the same instance every time; evaluating it in a hot loop allocates nothing. This is HotSpot's behaviour, not a promise of the language specification, so code must never depend on lambda identity, but it is a useful performance fact.
A lambda that captures locals needs somewhere to keep their values, so each evaluation creates a new object with fields for them. The allocation is small, and the JIT can often remove it through escape analysis when the lambda does not leave the method, but in general we should assume one small object per evaluation.
A lambda that uses an instance field or calls an instance method captures this. It is a capturing lambda whose captured value is the enclosing object, so each evaluation allocates an object that holds a reference to it. Method references such as this::render, or a bound reference like obj::method, capture their receiver in the same way.
4When Capturing this Becomes a Memory Leak
Capturing this matters when the lambda lives longer than the object it came from. The classic case is a listener registered with a long-lived event bus.
Each widget is discarded by our code, but the bus still holds the listener, the listener holds the widget through its captured this, and the widget holds its 10 MB buffer. None of them can be collected. A heap dump shows the chain from the bus's listener list to each widget.
The fix is to make the subscription's lifetime explicit. The subscribe call returns a handle, or we keep the listener in a field, and we unsubscribe when the widget is closed, ideally by making it AutoCloseable so try-with-resources or the framework's lifecycle does it. Weak listeners are another option in frameworks that support them. Notice that rewriting the lambda as this::render changes nothing, because the method reference captures this too.
5Follow-up questions
How can capturing this cause a memory leak with listeners?
When a lambda calls an instance method or uses a field, it holds a reference to the enclosing object. If that lambda is registered as a listener on something long-lived, such as a global event bus or a static registry, the registry keeps the lambda, the lambda keeps the object, and the object keeps everything it references, so none of it can be collected. The fix is to unsubscribe when the object's life ends, typically in close(), or to use weak listeners.
Do anonymous inner classes behave the same way?
Worse. An anonymous inner class created in an instance method always holds a reference to the enclosing instance, whether it uses it or not. A lambda captures this only if its body actually uses it, which is one quiet advantage of lambdas.
Does the bootstrap make the first call slow?
The first evaluation of each lambda call site does some work: it runs the bootstrap and generates a hidden class. That cost shows up in startup time for applications with thousands of lambdas. CDS archives and ahead-of-time features in recent JDKs reduce it, and after linking, evaluations are as cheap as a normal allocation or less.
6Conclusion
In this answer, we saw that javac turns each lambda body into a private synthetic method, static unless it uses this, and emits an invokedynamic instruction. On first execution, LambdaMetafactory generates a hidden class implementing the interface and links the call site to a factory. Non-capturing lambdas typically return one shared instance with no allocation, lambdas that capture locals allocate a new instance holding the values as fields, and lambdas that use instance members capture this, which can keep the enclosing object alive as long as the lambda, as in the listener leak.
Open with
At compile time, javac moves the lambda's body into a private synthetic method of the enclosing class, with a name like lambda$main$0. Captured local values become extra leading parameters, and the method is static unless the body uses this.
Then, if they want more
Where the lambda appears, javac emits an invokedynamic instruction. The first time it runs, its bootstrap method, LambdaMetafactory.metafactory, generates a small hidden class that implements the functional interface, has a field for each captured value, and calls the synthetic method. The call site is then linked to a factory, so later evaluations skip the bootstrap.
The differences come from what is captured. A non-capturing lambda has no state, so in HotSpot the call site returns the same instance every time and there is no allocation, though the spec does not promise identity. A lambda capturing locals allocates a new instance per evaluation holding copies of the values, which escape analysis can sometimes remove.
A lambda that uses an instance field or method captures this, so the object holds a reference to the enclosing instance. That is where leaks come from: if I register a lambda that calls one of my methods as a listener on a long-lived event bus, the bus keeps the lambda and the lambda keeps my whole object alive. The fix is to unsubscribe on close, or use weak listeners.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 15, Java 7
- 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
- java.util.stream, package documentation, java.util.function, package 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 "How does a lambda capture variables at the bytecode level? What is the difference...": https://scrivio-interview-library.netlify.app/java/lambdas/076094aef7/