The Artima Developer Community
Sponsored Link

Java Answers Forum
Difference between System.gc( ) and System.runFinalization( ) methods

1 reply on 1 page. Most recent reply: May 29, 2002 2:51 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Java

Posts: 3
Nickname: artima
Registered: May, 2002

Difference between System.gc( ) and System.runFinalization( ) methods Posted: May 28, 2002 9:41 PM
Reply to this message Reply
Advertisement
Hi All,
I want to know what is the difference between System.gc( ) and System.runFinalization( ) methods.

TIA


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Difference between System.gc( ) and System.runFinalization( ) methods Posted: May 29, 2002 2:51 PM
Reply to this message Reply
The thing is really simple !
The System.gc(); hints the VM that it is probably time to activate the Thread doing to the Garbage Collection. So all the part of this sentence stands in the hint word.

The finalizer are run according to the VM good will, generally speaking. This means they could be run or could not. Invoking the System.runFinalization( ); force the VM to invoke on each instance the finalizer when it Garbage collects the Object referenced....

Is it enough ?
See below the example !
Remember to install the Log4J library (http://jakarta.apache.org/log4j/)

Thomas SMETS,
SCJP2 - Brussels

import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.spi.LoggingEvent;
 
/**
  * This class tends to show that the <code>finalize</code>r are effectively 
  * run, but no <code>finalize</code>r of the unreachable chains of 
  * <code>Object</code>s are GCed.
  * 
  * In its current state the program should not show anything in the console
  * 
  * <pre>    
  *       public class FinalizedClass
  *       {
  *         public Category log;
  *         
  *         public FinalizedClass ()
  *         {
  *           log = Category.getInstance(FinalizedClass.class);      
  *         }
  *         
  *         public void finalize()
  *         {
  *           log.warn("finalize() run");
  *         }
  *       }
  * </pre>      
  * 
  * The code has been successfully tested on the JDK's 1.2.2, 1.3, 1.4  
  * 
  * @author <a href="mailto:tsmets@lautre.net">Thomas SMETS</a>
  * @version 0.3 - final
  * 
  */
public class ThreadedExampleWithoutGC
  implements Runnable
{
  public Category log;
  public int i;
  
  public static final int SLEEP_TIME = 1000;
  public static final int SPAWN_EVERY = 100;
  public static final int ARRAY_SIZE = 100000;
  
  ThreadedExampleWithoutGC (int i)
  {
    log = Category.getInstance(ThreadedExampleWithoutGC.class);
    this.i = i;
    log.info ("ThreadedExampleWithoutGC " + i + " created");
  }
    
  public void run()
  {    
    int j = 0;
    int k;
    FinalizedClass o[];
    while (true)
    {
      o = new FinalizedClass[ARRAY_SIZE];  // Allocate the space for the Object
      try 
      {
        log.info("Hi, I am " + i + " this is my " + j++ + "th loop");
        for (k = 0; k < ARRAY_SIZE; k++)         
          o[k] = new FinalizedClass();    // 
        
        Thread.sleep(SLEEP_TIME);
      }
      catch (Exception ex) 
      {
        log.warn("Ooops I got an exception", ex);
      }
      
      for (k = 0; k < ARRAY_SIZE; k++)       // Simply dereferences the Custom Object
        o[k] = null;      
    }
  }
  
/**
  * The provided implementation of the finalize method simply allow 
  * to put Logging statement in it !   
  */    
  public void finalize ()
  {
    log.warn ("Running the finalizer for ThreadedExampleWithoutGC number " + this.i);
  }  
  
  public static void main (String[] args)
    throws Exception
  {
     int i = 0;
     BasicConfigurator.resetConfiguration();
     //BasicConfigurator.configure(new CustomConsoleAppender(Priority.INFO));
     BasicConfigurator.configure ();
     System.runFinalization();
     Thread t =null;
     do             // This "infinite" do-loop will generate java.lang.OutOfMemoryError
     {              // The program is therefore bound to crash before some of 
        try         // the finalizer are run
        {
          t = new Thread(new ThreadedExampleWithoutGC(i++));
          t.start();
          Thread.sleep(SPAWN_EVERY);
          t = null; // To make sure that no reference to the active Thread still exist.
        } catch (Exception ex) 
        { // Just in case the System runs out of Memory
          System.out.print("Exception " + ex.getClass());
        }        
     } while (true);       
  } // End of main
  
  /**
    * This class is there simply to allow an <code>Object</code> finalization 
    * to be logged with its own <code>Level</code> and space of logging.
    */
  public class FinalizedClass
  {
    public Category log;
    
    public FinalizedClass ()
    {
      log = Category.getInstance(FinalizedClass.class);      
    }
    
    public void finalize()
    {
      log.info("finalize() run");
    }
  } // End of inner class FinalizedClass
  
  /**
    *
    * This class allows for a quickly <i>custom logging</i> to the Console.
    * Any statement above the <code>Priority</code> level passed as
    * argument ot the constructor will be given to the parent 
    * <code>ConsoleAppender</code> for Logging.
    * 
    * I took this approache as I am not used to <code>PropertyConfigurator</code>, 
    * yet.
    * 
    * (Inner) Class is <code>static</code> for simplicity's sake. 
    * 
    */
  static class CustomConsoleAppender 
    extends ConsoleAppender
  {     
    Priority LoggingPriority = null;
    
    CustomConsoleAppender(Priority aLoggingLevel)
    {
      LoggingPriority = aLoggingLevel;
    }
    
    public void doAppend(LoggingEvent aLoggingEvent) 
    {
      if (aLoggingEvent.level.isGreaterOrEqual(LoggingPriority))
        super.doAppend(aLoggingEvent);
      return;    
    }
  } // End of Inner class CustomConsoleAppender 
}   // End of Class

Flat View: This topic has 1 reply on 1 page
Topic: ResultSet problem Previous Topic   Next Topic Topic: My tabbed panes won't appear. :^(

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use