1
2
3
4
5
6
7
8
9
10
11
12
13
14
package test;

// 默认隐士 extends Object
public class Stu {

// 并不是这样说的,Java调用了finalize函数,代表这个类可能被回收了(回收的前兆)
// 此类加入到queue队列中,等待虚拟机来处理(只有虚拟机处理回收了,才叫回收)
@Override
protected void finalize() throws Throwable {
super.finalize();

System.out.println("Java 我被回收了");
}
}

JavaHelp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package test;

import java.io.Closeable;
import java.io.IOException;

// 隐士 extends Object
public class JavaHelp implements Closeable {


@Override
protected void finalize() throws Throwable {
super.finalize();
close();
}

@Override
public void close() throws IOException {

}
}

Student.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package test


// 隐士的 : Any?
// KT做不大,但是可以借助Java来做
class Student : JavaHelp() {

override fun close() {
super.close()

println("KT 我被回收了");
}


}

main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package test;

public class Main {

public static void main(String[] aaa) {

// ====== Java 回收

Stu stu = new Stu();

stu = null;
System.gc();; // gc机制来扫描吧,gc一过来发现,stu还有指向,扫你妹啊


// ===== KT 回收

Student student = new Student();
student = null;
System.gc();;
}

}