System.gc()为什么没有调用finalize()方法?
//下列程序为什么没有调用finalize()?
//改 Book novel=new Book(true) 为 new Book(true) 则调用finalize(),为什么?
//咨询finalize()和System.gc()使用经验!!
class Book {
boolean checkedOut=false ;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
public void finalize() {
if(checkedOut)
System.out.println("Error: checked out");
}
}
public class TerminationCondition {
public static void main(String[] args) {
new Book(true);
System.gc();
}
}
//改 Book novel=new Book(true) 为 new Book(true) 则调用finalize(),为什么?
//咨询finalize()和System.gc()使用经验!!
class Book {
boolean checkedOut=false ;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
public void finalize() {
if(checkedOut)
System.out.println("Error: checked out");
}
}
public class TerminationCondition {
public static void main(String[] args) {
new Book(true);
System.gc();
}
}
...全文
相关推荐
JAVA程序员面试题(含有答案)经典版
有两种回收机制:自动回收和手工调用 System.gc() 方法。实际上调用 System.gc() 就相当于调用 Runtime.getRuntime().gc() 方法。3.
【java】调用System.gc()会发生什么?
1.概述 转载:调用System.gc()会发生什么? System.gc(); //告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的 System.runFinalization(); //强制调用已经失去引用的对象的finalize方法 查看源码 当我们调用System.gc()的时候,其实并不会马上进行垃圾回收,甚至不一定会执行垃圾回收,查看系统源码可以看到 /** * Indicates to the VM that it would be a good time to run.
中软国际Java程序员笔试题及答案.pdf
要请求垃圾收集,可以调用System.gc()或Runtime.getRuntime().gc()方法。11.
Java常见面试题(含答案)
在Java中,可以通过以下方式请求垃圾收集: ```java System.gc(); // 请求垃圾收集 Runtime.getRuntime().gc(); // 请求垃圾收集 ```#### 9.
java学习笔记
- 用户可以通过调用`System.gc()`或`Runtime.getRuntime().gc()`来请求进行垃圾回收,但这并不保证立即执行。


