The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

its a blurred but good question

Posted by somebody on May 28, 2001 at 8:00 PM

> Now I think the behaviour of Garbage collection is changed as was mentioned in this example by Bruce Eckel, i need to confirm this
> cause No matters how u run this program with command line arguments or without command line arguments the result is same that is all the created objects are finalized and there is never a need to run the System.gc method or System.runFinalization method.
> if there is any updation to the that is required?
> plz help
> Sincerely
> Bilal
> class Chair{
> static boolean gcrun = false;
> static boolean f = false;
> static int finalized =0;
> static int created =0;
> int i;
> Chair(){
> i=++created;
> if(created ==47)
> System.out.println("Created 47");
> }

> public void finalize(){
> if(!gcrun){
> //the first time finalize () is called:
> gcrun= true;
> System.out.println(
> "Beginnning 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");
> }
> }
> public class Garbage{
> public static void main (String [] args){
> // As long as the flag hasn't been set,
> // make chairs and strings:
> while(!Chair.f){
> new Chair();
> new String("To take up space this step is added");
> }
> System.out.println( "After all chairs have been created: \n"+
> "total created = "+Chair.created+
> " total finalized +" +Chair.finalized);
> /*optional arguments force garbage collection & finalizatiion*/

> try{
> if(args[0].equals("gc") || args[0].equals("all")){
> System.out.println("gc(): ");
> System.gc();
> }
> if(args[0].equals(" finalize") ||
> args[0].equals("all")){
> System.out.println(" runfinalization():");
> System.runFinalization();
> }
> System.out.println("bye!");
> }catch (ArrayIndexOutOfBoundsException e){
> System.out.println("zastaserey exception tha");}
> }
> }
> // well it seems that this topic of thinking in java is a bit old now changes have came in the way the garbage collection works.'
> // what is the reason that this program always show number of finalized objects even one more// //than the number of objects create

> // third this programs generate and exception that is there is some error in the code






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us