1 12 17 package org.openbravo.base; 18 import java.io.*; 19 import org.apache.log4j.Logger ; 20 21 26 public class PeriodicGarbageCollector implements Runnable { 27 static Logger log4j = Logger.getLogger(PeriodicGarbageCollector.class); 28 private Thread runner; 29 private long seconds; 30 31 35 public PeriodicGarbageCollector(long seconds) throws IOException 36 { 37 runner = new Thread (this); 39 runner.start(); 40 this.seconds = seconds; 41 } 42 43 44 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 } catch(InterruptedException e) { 69 return; 74 } 75 } 76 77 } 79 public String mostrarMemoria(String texto) { 80 return (texto + "\n" + mostrarMemoria()); 81 } 82 83 public String mostrarMemoria() { 84 StringBuffer result = new StringBuffer (); 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 } 94 | Popular Tags |