HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Avoid Bothersome Garbage Collection Pauses"
Vol. 8, Issue 7, p. 44

	



Listing 1

1 public class BetterControlOfGC {
2	  	 // only schedule garbage collection for the application once
3   	private static boolean done = false ;
4
5	   public static void  suggestGCNow ()
6	   {
7	        System.gc () ;
8	   }
9
10    private class GCStimulatorTask extends java.util.TimerTask {
11      public void run () {
12	        suggestGCNow () ;
13	     }
14	   }
15
16	    private static GCStimulatorTask instance = null ;
17
18     private synchronized GCStimulatorTask getInstance () {
19		    if (instance == null ) {
20		         instance = new GCStimulatorTask () ;
21        }
22 		 return instance ;
23	    }
24
25  	 public  void scheduleRegularGC (long intervalMilliSecs ) {
26    	if (!done){ // only schedule 1 garbage collector per appl     
27			  GCStimulatorTask stimulator = getInstance () ;
28			  java.util.Timer scheduler = new java.util.Timer () ;
29			  scheduler.scheduleAtFixedRate (stimulator, 10, 30
30						intervalMilliSecs);
31		     done = true ;
32   	  	}
33		 	else
34 		{
35       	 System.err.println("GC Task already scheduled.") ;
36   		}
37  	 }
38
39     public BetterControlOfGC () {}
40   }

 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.