垃圾回收
This commit is contained in:
parent
886c549976
commit
a6c121d9a1
28
src/chapter5/Example13.java
Normal file
28
src/chapter5/Example13.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package chapter5;
|
||||||
|
|
||||||
|
class Person implements AutoCloseable {
|
||||||
|
String name;
|
||||||
|
public Person(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
System.out.println( name + " 要被回收啦!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Example13 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person p1 = new Person("小明");
|
||||||
|
Person p2 = new Person("大王");
|
||||||
|
|
||||||
|
p1 = null;
|
||||||
|
p2 = null;
|
||||||
|
|
||||||
|
System.gc();
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000000; i ++) {
|
||||||
|
// 为了延长程序运行的时间
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
src/chapter5/Example14.java
Normal file
4
src/chapter5/Example14.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package chapter5;
|
||||||
|
|
||||||
|
public class Example14 {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user