参考:https://blog.csdn.net/a4171175/article/details/90749839
package reborn;
import java.util.HashSet;
public class hello {
public static void main(String[] args) {
if(args.length == 0) {
System.err.println("Usage: \n" +
"java Garbage before\n or:\n" +
"java Garbage after");
return;
}
while(!Chair.f) {
new Chair();
new String("To take up space");
}
System.out.println(
"After all Chairs have been created:\n" +
"total created = " + Chair.created +
", total finalized = " + Chair.finalized);
if(args[0].equals("before")) {
System.out.println("gc():");
System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
}
package reborn;
class Chair{
static boolean gcrun = false;
static boolean f = false;
static int created = 0;
static int finalized = 0;
int i;
Chair() {
i = ++created;
if(created == 47)
System.out.println("Created 47");
}
protected void finalize() {
if(!gcrun) {
gcrun = true;
System.out.println(
"Beginning to finalize after " +
created + " Chairs have been created");
}
if(i == 47) {
System.out.println(
"Finalizing Chair #47, " +
"Setting flag to stop Chair creation");
f = true;
}
finalized++;
if(finalized >= created)
System.out.println(
"All " + finalized + " finalized");
}
}
输出:
Created 47
Beginning to finalize after 58203 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 268731, total finalized = 34257
bye!
//before
Created 47
Beginning to finalize after 58223 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 268546, total finalized = 34150
gc():
runFinalization():
bye!
//after
Created 47
Beginning to finalize after 58343 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 268676, total finalized = 34154
bye!
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.A
subclass overrides the finalize method to dispose of
system resources or to perform other cleanup.
finalize方法:
The general contract of finalize is that it is invoked if and when the Java™ virtualmachine has determined that there is no longer anymeans by which this object can be accessed by any thread that hasnot yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purposeof finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize methodfor an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded. The finalize method of class Object performs nospecial action; it simply returns normally. Subclasses of Object may override this definition. The Java programming language does not guarantee which thread willinvoke the finalize method for any given object. It isguaranteed, however, that the thread that invokes finalize will notbe holding any user-visible synchronization locks when finalize isinvoked. If an uncaught exception is thrown by the finalize method,the exception is ignored and finalization of that object terminates. After the finalize method has been invoked for an object, nofurther action is taken until the Java virtual machine has againdetermined that there is no longer any means by which this object canbe accessed by any thread that has not yet died, including possibleactions by other objects or classes which are ready to be finalized,at which point the object may be discarded. The finalize method is never invoked more than once by a Javavirtual machine for any given object. Any exception thrown by the finalize method causesthe finalization of this object to be halted, but is otherwiseignored.