Backend Development 8 min read

Do Lambda Expressions Cause Memory Leaks Like Anonymous Inner Classes in Java?

This article analyzes whether Java lambda expressions can lead to memory leaks similar to anonymous inner classes by comparing their compiled bytecode, demonstrating that lambdas only retain a reference to the outer class when they explicitly capture its members.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Do Lambda Expressions Cause Memory Leaks Like Anonymous Inner Classes in Java?

Anonymous inner classes in Java hold an implicit reference to their outer class, which can cause memory leaks; the article investigates whether lambda expressions share this risk.

Two methods are defined in a TestInner class: test() uses a lambda expression, while test1() uses an anonymous inner class.

public class TestInner {
    public void test() {
        new Thread(()->{
            Log.i("测试","dddd");
        }).start();
    }
    public void test1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("测试","dddd1");
            }
        }).start();
    }
}

Compiling test1() reveals a synthetic class TestInner$1 whose constructor receives the outer TestInner instance, confirming the retained reference.

.method public test1()V
    .registers 3
    .line 14
    new-instance v0, Ljava/lang/Thread;
    new-instance v1, Lcom/example/jnihelper/TestInner$1;
    invoke-direct {v1, p0}, Lcom/example/jnihelper/TestInner$1;->
(Lcom/example/jnihelper/TestInner;)V
    invoke-direct {v0, v1}, Ljava/lang/Thread;->
(Ljava/lang/Runnable;)V
    .line 19
    invoke-virtual {v0}, Ljava/lang/Thread;->start()V
    .line 20
    return-void
.end method

The lambda version compiles to a synthetic class TestInner$$ExternalSyntheticLambda0 that does not receive the outer instance, so no reference is kept.

.method static synthetic Lambda表达式()V
    .registers 2
    .line 9
    const-string v0, "\u6d4b\u8bd5"
    const-string v1, "dddd"
    invoke-static {v0, v1}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I
    .line 10
    return-void
.end method

.method public test()V
    .registers 3
    .line 8
    new-instance v0, Ljava/lang/Thread;
    sget-object v1, Lcom/example/jnihelper/TestInner$$ExternalSyntheticLambda0;->INSTANCE:Lcom/example/jnihelper/TestInner$$ExternalSyntheticLambda0;
    invoke-direct {v0, v1}, Ljava/lang/Thread;->
(Ljava/lang/Runnable;)V
    .line 10
    invoke-virtual {v0}, Ljava/lang/Thread;->start()V
    .line 11
    return-void
.end method

When the lambda captures an outer field (e.g., helloInner ), the compiler generates a synthetic class that stores the outer instance, making the lambda hold a reference and potentially causing a leak.

public class TestInner {
    private String helloInner = "helloIIIII";
    public void test() {
        new Thread(() -> {
            Log.i("测试", "dddd");
            Log.i("测试", TestInner.this.helloInner); // explicit capture
        }).start();
    }
}

In this case the generated TestInner$$ExternalSyntheticLambda0 contains a field f$0 referencing the outer TestInner instance.

.field public final synthetic f$0:Lcom/example/jnihelper/TestInner;
.method public synthetic constructor
(Lcom/example/jnihelper/TestInner;)V
    iput-object p1, p0, Lcom/example/jnihelper/TestInner$$ExternalSyntheticLambda0;->f$0:Lcom/example/jnihelper/TestInner;
    return-void
.end method

Therefore, anonymous inner classes always retain the outer reference, while lambda expressions only do so when they explicitly capture outer members.

Conclusion: Anonymous inner classes pose a memory‑leak risk; lambdas are safe unless they capture the outer class, in which case they behave like inner classes.

JavaAndroidbytecodelambdaMemory LeakAnonymous Inner Class
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.