Developer's Daily Java Education - Test Projects
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
   
 


/**
 * Title:        

StaticClassRamUsage * Description: Test of when a static class is instantiated * and when it is garbage collected

* Copyright: Copyright (c) Al Alexander

* Company:

* @author Al Alexander * @version 1.0 */ public class Main { static { System.err.println( "in static initializer in Main..." ); System.err.println( " free memory: " + Runtime.getRuntime().freeMemory() ); } public Main() { System.err.println( "just entered Main(), about to create a SecondClass object" ); System.err.println( " free memory: " + Runtime.getRuntime().freeMemory() ); SecondClass sc = new SecondClass(); sc = null; System.err.println( "now leaving Main() ..." ); } public static void main(String[] args) { System.err.println( "just started in Main.main()" ); System.gc(); System.err.println( " in Main.main(), free memory: " + Runtime.getRuntime().freeMemory() ); Main main1 = new Main(); System.err.println( "in Main.main(), calling garbage collector" ); System.gc(); System.err.println( " free memory: " + Runtime.getRuntime().freeMemory() ); System.err.println( "entering for loop in Main.main()" ); for (int i=0; i<10; i++) { System.err.print( "zzz ..." ); try { Thread.sleep(500); } catch (Exception e) {} } System.err.print( "\n" ); System.err.println( "free memory: " + Runtime.getRuntime().freeMemory() ); System.err.println("this is the last line of Main.main(), program is over"); } }

Copyright 1998-2003 DevDaily Interactive, Inc.
All Rights Reserved.