Kbase 19692: OPENCLIENT: How to force the Java Virtual Machine (JVM) to continually invoke both finalization and
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  16/10/2008 |
|
Status: Verified
GOAL:
How to force the Java Virtual Machine (JVM) to continually invoke both finalization and garbage collection from a background thread.
FIX:
By default, the following code will execute finalization and garbage collection every 1,000 milliseconds. You can adjust this amount by changing the value of the field named 'delay':
public class ForcedGarbageCollection
{
private Thread gcThread = null;
private int delay = 1000;
/**
* ForcedGarbageCollection constructor
*/
public ForcedGarbageCollection()
{
super();
Runnable runGC = new Runnable()
{
public void run()
{
try
{
runFinalizationAndGC();
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
gcThread = new Thread(runGC);
gcThread.setDaemon(true);
gcThread.start();
}
/**
* This method does the actual finalization and gc calls
*/
private void runFinalizationAndGC()
{
while (true)
{
try
{
System.runFinalization();
System.gc();
Thread.sleep(delay);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
}