因为runtime.gc();调用后并不一定会马上执行。
答案1、
    执行完tf = null;这以后,表明对象tf可以被回收,并在调用runtime.gc();以后被回收,因此finalize()只被执行了一次,所以。
答案2、
    执行完tf = null;这以后,表明对象tf可以被回收,且系统在这时回收,调用一次finalize(),接着又人工调用了runtime.gc();finalize()又被执行了一次,所以。
答案3、
同上面差不多,只是很“巧”而已。————————————————————————
如有不对之处,。。还请大家指正。 :)
————————————————————————

解决方案 »

  1.   

    gaojunbo(飞马) :
    能给讲一下
    Runtime runtime = Runtime.getRuntime();
    runtime.gc();
    的功能吗?
      

  2.   

    Runtime 是一个类,且这个类没有构造方法。如要得到这个类的实例可以用getRuntime()方法。
    因此这一句就是实例化Runtime类。
    Runtime runtime = Runtime.getRuntime();
    Runtime类有一个gc()方法,这个方法要求JVM执行垃圾回收,但不一定会马上执行,什么时候执行由系统决定。
      

  3.   

    gaojunbo(飞马) :为什么人工调用了runtime.gc();后finalize()又被执行了一次呢?
      

  4.   

    Constructors and finalizersEvery Java class has a special purpose method called a constructor. The constructor always has the same name as the class and it can't specify a return value. The constructor allocates all the resources needed by the object and returns an instance of the object. When you use the new operator, you are actually calling the constructor. You don't need to specify a return type for the constructor because the instance of the object is always the return type. Most object-oriented languages have a corresponding method called a destructor that is called to deallocate all the resources that the constructor allocated. But because Java deallocates all the resources for you automatically, there is no destructor mechanism in Java. There are situations, however, that require you to perform some special cleanup that the garbage collector can't handle as the class goes away. For example, you might have opened some files in the life of the object and you want to make sure the files are closed properly when the object is destroyed. There is another special purpose method that can be defined for a class called a finalizer. This method (if present) is called by the garbage collector immediately before the object is destroyed. Therefore, if there is any special cleanup that needs to be performed, the finalizer can handle it for you. The garbage collector runs as a low priority thread in the virtual machine, however, so you can never predict when it will actually destroy your object. So, you shouldn't put any time-sensitive code in the finalizer because you can't predict when it will be called.