Adding new listener to defragement heap at startup.

This commit is contained in:
bdc34 2010-10-18 19:06:27 +00:00
parent e8fed3dfbf
commit 5951f16be0

View file

@ -0,0 +1,40 @@
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class will request a full garbage collection when
* contextInitialized() is called. The goal is to eliminate fragmentation
* in the tenured generation and avoid problems with the 'young generation guarantee.'
*
* This should be the last listener before the context starts.
*
* See http://blogs.sun.com/jonthecollector/entry/when_the_sum_of_the (retrieved 2010-10-18)
*
* @author bdc34
*
*/
public class HeapDefragement implements ServletContextListener {
private static final Log log = LogFactory.getLog(HeapDefragement.class);
@Override
public void contextInitialized(ServletContextEvent arg0) {
try{
log.info("Calling System.gc() to defragement the heap.");
long start = System.currentTimeMillis();
System.gc();
log.info("GC took " + (System.currentTimeMillis() - start) + " msec");
}catch(Exception ex){
log.error(ex,ex);
}
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
//do nothing
}
}