Why do anonymous inner classes leak, why don't Lambdas leak

In Android development, there are actually two main scenarios where memory leaks occur. One is the problem of too large data, but the problem of inconsistency between the calling and called life cycles. Leaks caused by inconsistent object life cycles account for 90%, the most common The one that is not easy to analyze is the memory leak of the anonymous inner class. In the article "#Memory Leak Big Collection: Performance Optimization Skills Android Developers Can't Miss", I probably summarized it. Recently, I encountered a problem during development. It is the memory leak detected by LeakCannry. The principle of LeakCannry detection is probably realized by the GC reachability algorithm. One of the most common problems in our products is caused by anonymous inner classes.

Cases that do not involve states holding external class references

How anonymous inner classes can cause memory leaks

In the Java system, there are many kinds of inner classes. The most common ones are static inner classes and anonymous inner classes. In general, it is recommended to use static inner classes. Why is this? Let’s look at an example first:

public class Test {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
    }
}

The reason for the leak of the anonymous inner class: the inner class holds a reference to the outer class. In the above scenario, when the outer class is destroyed, the anonymous inner class Runnable will cause a memory leak.

verify this conclusion

The class file of the above code looks like this after viewing it through Javap -c

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/Thread
       3: dup
       4: new           #3                  // class Test$1
       7: dup
       8: invokespecial #4                  // Method Test$1."<init>":()V
      11: invokespecial #5                  // Method java/lang/Thread."<init>":(Ljava/lang/Runnable;)V
      14: invokevirtual #6                  // Method java/lang/Thread.start:()V
      17: return
}

Let's look directly at the instructions in the main method:

0: new #2 // 创建一个新的 Thread 对象 
3: dup // 复制栈顶的对象引用 
4: new #3 // 创建一个匿名内部类 Test$1 的实例 
7: dup // 复制栈顶的对象引用 
8: invokespecial #4 // 调用匿名内部类 Test$1 的构造方法 
11: invokespecial #5 // 调用 Thread 类的构造方法,传入匿名内部类对象 
14: invokevirtual #6 // 调用 Thread 类的 start 方法,启动线程 
17: return // 返回

We can see that an instance of Test$1 is created using the new instruction in step 4, and in step 8, the constructor of the anonymous inner class is invoked through the invokespecial instruction, so that the generated inner class will hold The reference of the outer class, so the outer class cannot be recycled, will cause a memory leak.

Why Lambda doesn't leak

At first, I thought that Lambda was just syntactic sugar and would not have other functions. However, haha, everyone probably already thought of it.

Anonymous inner class will not cause memory leak when using Lambda.

Look at the code:

public class Test {
    public static void main(String[] args) {
        new Thread(() -> {

        }).start();
    }
}

Change the above code to Lambda format

class file:

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/Thread
       3: dup
       4: invokedynamic #3,  0              // InvokeDynamic #0:run:()Ljava/lang/Runnable;
       9: invokespecial #4                  // Method java/lang/Thread."<init>":(Ljava/lang/Runnable;)V
      12: invokevirtual #5                  // Method java/lang/Thread.start:()V
      15: return
}

At first glance, the answer is already known, no inner class is generated in this bytecode,

在Lambda格式中,没有生成内部类,而是直接使用invokedynamic 指令动态调用run方法,生成一个Runnable对象。再调用调用Thread类的构造方法,将生成的Runnable对象传入。从而避免了持有外部类的引用,也就避免了内存泄漏的发生。

在开发中,了解字节码知识还是非常有必要的,在关键时刻,我们查看字节码,确实能帮助自己解答一些疑惑,下面是常见的一些字节码指令

常见的字节码指令

Java 字节码指令是一组在 Java 虚拟机中执行的操作码,用于执行特定的计算、加载、存储、控制流等操作。以下是 Java 字节码指令的一些常见指令及其功能:

  1. 加载和存储指令:
  • aload:从局部变量表中加载引用类型到操作数栈。
  • astore:将引用类型存储到局部变量表中。
  • iload:从局部变量表中加载 int 类型到操作数栈。
  • istore:将 int 类型存储到局部变量表中。
  • fload:从局部变量表中加载 float 类型到操作数栈。
  • fstore:将 float 类型存储到局部变量表中。
  1. 算术和逻辑指令:
  • iadd:将栈顶两个 int 类型数值相加。
  • isub:将栈顶两个 int 类型数值相减。
  • imul:将栈顶两个 int 类型数值相乘。
  • idiv:将栈顶两个 int 类型数值相除。
  • iand:将栈顶两个 int 类型数值进行按位与操作。
  • ior:将栈顶两个 int 类型数值进行按位或操作。
  1. 类型转换指令:
  • i2l:将 int 类型转换为 long 类型。
  • l2i:将 long 类型转换为 int 类型。
  • f2d:将 float 类型转换为 double 类型。
  • d2i:将 double 类型转换为 int 类型。
  1. 控制流指令:
  • if_icmpeq:如果两个 int 类型数值相等,则跳转到指定位置。
  • goto:无条件跳转到指定位置。
  • tableswitch:根据索引值跳转到不同位置的指令。
  1. 方法调用和返回指令:
  • invokevirtual:调用实例方法。
  • invokestatic:调用静态方法。
  • invokeinterface:调用接口方法。
  • ireturn:从方法中返回 int 类型值。
  • invokedynamic: 运行时动态解析并绑定方法调用

详细的字节码指令列表和说明可参考 Java 虚拟机规范(Java Virtual Machine Specification)

总结

为了解决问题而储备知识,是最快的学习方式。

In development, don’t deliberately design invokedynamic code, but students who develop Java, Lambda is a must.

Guess you like

Origin juejin.im/post/7244002037192081468