在测试的路上,学习无止境。。。 相信自己,快乐每一天。。。 紧急通知: 支付宝急招资深软件测试工程师,详见 http://job.alipay.com/recruit/offerDetail.htm?offerId=21683,同时招聘资深测试开发工程师。有意者投递简历至:xin.maojx@alipay.com

finalize()的调用

上一篇 / 下一篇  2009-08-22 16:36:12 / 个人分类:JAVA 学习

今天学习了finalize()的调用,先看例子:

package exceise;

import java.util.*;

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 Exceise3 {
 public static void main(String[] args) {
 Book novel = new Book(true);
 // Proper cleanup:
 novel.checkIn();
 // Drop the reference, forget to clean up:
 new Book(true);
 // Force garbage collection & finalization:
 System.gc();
 }
 } ///:~

 

输出:Error: checked out

只要调用了System.gc() 它就会收尾和破坏到当前为止不再使用的所有对象
请记住无论垃圾收集还是收尾都并非肯定会运行,只要Java 虚拟机JVM 还没达
到内存即将用光的地步那么通常不会浪费时间通过垃圾收集机制来回收内存空间。

 

 


TAG: java JAVA

 

评分:0

我来说两句

Open Toolbar