KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > base > PeriodicGarbageCollector


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 /**
13  * PeriodicGarbageCollector.
14  * @version 1.0.0 11/5/2004
15  * @author Carlos Romero Herrero
16 */

17 package org.openbravo.base;
18 import java.io.*;
19 import org.apache.log4j.Logger ;
20
21 /**
22  * PeriodicGarbageCollector
23  * @version 1.0.0 11/5/2004
24  * @author Carlos Romero Herrero
25 */

26 public class PeriodicGarbageCollector implements Runnable JavaDoc {
27   static Logger log4j = Logger.getLogger(PeriodicGarbageCollector.class);
28   private Thread JavaDoc runner;
29   private long seconds;
30
31   /**
32    * Creates a new periodic garbage collector<br>
33    * time: Number in seconds between two garbage collections<br>
34    */

35   public PeriodicGarbageCollector(long seconds) throws IOException
36   {
37     // Fire up the background housekeeping thread
38
runner = new Thread JavaDoc(this);
39     runner.start();
40     this.seconds = seconds;
41   }
42
43
44   /**
45    * Housekeeping thread. Runs in the background with low CPU overhead.
46    * This method acts as fault tolerance for bad connection/statement programming.
47    */

48   public void run()
49   {
50     boolean forever = true;
51
52     while(forever) {
53       try {
54         if (log4j.isDebugEnabled()) log4j.debug("Sleeping for "+seconds+" seconds.");
55         Thread.sleep((seconds*1000));
56         if (log4j.isDebugEnabled()) log4j.debug("gc begins");
57         long inicio = System.currentTimeMillis();
58         System.gc();
59         long fin = System.currentTimeMillis();
60         if (log4j.isDebugEnabled()) log4j.debug("gc ended: "+(fin-inicio)+" miliseconds");
61         if (log4j.isDebugEnabled()) log4j.debug("Finalization begins");
62         inicio = System.currentTimeMillis();
63         System.runFinalization();
64         fin = System.currentTimeMillis();
65         if (log4j.isDebugEnabled()) log4j.debug("Finalization ended: "+(fin-inicio)+" miliseconds");
66         if (log4j.isDebugEnabled()) log4j.debug(mostrarMemoria());
67       } // Wait time miliseconds for next cycle
68
catch(InterruptedException JavaDoc e) {
69         // Returning from the run method sets the internal
70
// flag referenced by Thread.isAlive() to false.
71
// This is required because we don't use stop() to
72
// shutdown this thread.
73
return;
74       }
75     }
76
77   } // End run
78

79   public String JavaDoc mostrarMemoria(String JavaDoc texto) {
80     return (texto + "\n" + mostrarMemoria());
81   }
82
83   public String JavaDoc mostrarMemoria() {
84     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
85     result.append("Maximum memory :").append(Runtime.getRuntime().maxMemory());
86     result.append("Total memory :").append(Runtime.getRuntime().totalMemory());
87     result.append("Free memory :").append(Runtime.getRuntime().freeMemory());
88     result.append("% free memory:").append((100*Runtime.getRuntime().freeMemory()/Runtime.getRuntime().totalMemory())).append("%");
89     return result.toString();
90   }
91
92 }// End class
93

94
Popular Tags