org.apache.excalibur.store.impl
Class StoreJanitorImpl

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.excalibur.store.impl.StoreJanitorImpl
All Implemented Interfaces:
java.lang.Runnable, org.apache.avalon.framework.activity.Startable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.parameters.Parameterizable, org.apache.avalon.framework.thread.ThreadSafe, StoreJanitor

public class StoreJanitorImpl
extends org.apache.avalon.framework.logger.AbstractLogEnabled
implements StoreJanitor, org.apache.avalon.framework.parameters.Parameterizable, org.apache.avalon.framework.thread.ThreadSafe, java.lang.Runnable, org.apache.avalon.framework.activity.Startable

This class is a implentation of a StoreJanitor. Store classes can register to the StoreJanitor. When memory is too low, the StoreJanitor frees the registered caches until memory is normal.

A few parameters can be used:

Version:
CVS $Id: StoreJanitorImpl.java,v 1.4 2004/02/28 11:47:31 cziegeler Exp $
Author:
Avalon Development Team

Field Summary
private  boolean adaptiveThreadInterval
           
private  boolean doRun
           
private  boolean firstRun
          Flag to ignore memory bursts during startup (1st janitor run)
private  double fraction
           
private  int index
           
protected  long interval
          The calculated delay for the next checker run in ms
protected  long inUse
          Amount of memory in use before sleep().
protected  boolean invokeGC
          Should the gc be called on low memory?
private  java.lang.Runtime jvm
           
private  int maxHeapSize
           
private  long maxRateOfChange
          Used memory change rate in bytes per second
private  int minFreeMemory
           
private  int minThreadInterval
           
private  int priority
           
private  boolean secondRun
          Flag to ignore memory bursts during startup (2nd janitor run)
private  java.util.ArrayList storelist
           
private  int threadInterval
           
 
Fields inherited from interface org.apache.excalibur.store.StoreJanitor
ROLE
 
Constructor Summary
StoreJanitorImpl()
           
 
Method Summary
private  int calcToFree(Store store)
          This method claculates the number of Elements to be freememory out of the Cache.
protected  void checkMemory()
          The "checker" thread checks if memory is running low in the jvm.
private  void freeMemory()
          Round Robin alghorithm for freeing the registered caches.
private  void freePhysicalMemory()
          This method forces the garbage collector
private  boolean getAdaptiveThreadInterval()
           
private  int getIndex()
           
private  java.lang.Runtime getJVM()
           
private  int getMaxHeapSize()
           
private  int getMinFreeMemory()
           
private  int getPriority()
           
private  java.util.ArrayList getStoreList()
           
private  int getThreadInterval()
           
 java.util.Iterator iterator()
          This method return a java.util.Iterator of every registered stores The iterators returned is fail-fast: if list is structurally modified at any time after the iterator is created, in any way, the iterator will throw a ConcurrentModificationException.
private  long longDiv(long top, long bottom)
           
protected  long memoryInUse()
          Calculate the JVM memory in use now.
private  boolean memoryLow()
          Method to check if memory is running low in the JVM.
private  long minTimeToFill(long rate)
          Calculate amount of time needed to fill all free memory with given fill rate.
 void parameterize(org.apache.avalon.framework.parameters.Parameters params)
          Parameterize the StoreJanitorImpl.
 void register(Store store)
          This method register the stores
 void run()
          The "checker" thread loop.
private  void setIndex(int _index)
           
 void start()
           
 void stop()
           
 void unregister(Store store)
          This method unregister the stores
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

minFreeMemory

private int minFreeMemory

maxHeapSize

private int maxHeapSize

threadInterval

private int threadInterval

minThreadInterval

private int minThreadInterval

adaptiveThreadInterval

private boolean adaptiveThreadInterval

priority

private int priority

fraction

private double fraction

invokeGC

protected boolean invokeGC
Should the gc be called on low memory?


jvm

private java.lang.Runtime jvm

storelist

private java.util.ArrayList storelist

index

private int index

doRun

private boolean doRun

inUse

protected long inUse
Amount of memory in use before sleep(). Must be initially set a resonable value; ie. memoryInUse()


firstRun

private boolean firstRun
Flag to ignore memory bursts during startup (1st janitor run)


secondRun

private boolean secondRun
Flag to ignore memory bursts during startup (2nd janitor run)


interval

protected long interval
The calculated delay for the next checker run in ms


maxRateOfChange

private long maxRateOfChange
Used memory change rate in bytes per second

Constructor Detail

StoreJanitorImpl

public StoreJanitorImpl()
Method Detail

parameterize

public void parameterize(org.apache.avalon.framework.parameters.Parameters params)
                  throws org.apache.avalon.framework.parameters.ParameterException
Parameterize the StoreJanitorImpl.

Specified by:
parameterize in interface org.apache.avalon.framework.parameters.Parameterizable
Parameters:
params - the Configuration of the application
Throws:
org.apache.avalon.framework.parameters.ParameterException

start

public void start()
Specified by:
start in interface org.apache.avalon.framework.activity.Startable

stop

public void stop()
Specified by:
stop in interface org.apache.avalon.framework.activity.Startable

run

public void run()
The "checker" thread loop.

Specified by:
run in interface java.lang.Runnable

checkMemory

protected void checkMemory()
The "checker" thread checks if memory is running low in the jvm.


memoryLow

private boolean memoryLow()
Method to check if memory is running low in the JVM.

Returns:
true if memory is low

memoryInUse

protected long memoryInUse()
Calculate the JVM memory in use now.

Returns:
memory in use.

minTimeToFill

private long minTimeToFill(long rate)
Calculate amount of time needed to fill all free memory with given fill rate.

Parameters:
rate - memory fill rate in time per bytes
Returns:
amount of time to fill all the memory with given fill rate

longDiv

private long longDiv(long top,
                     long bottom)

register

public void register(Store store)
This method register the stores

Specified by:
register in interface StoreJanitor
Parameters:
store - the store to be registered

unregister

public void unregister(Store store)
This method unregister the stores

Specified by:
unregister in interface StoreJanitor
Parameters:
store - the store to be unregistered

iterator

public java.util.Iterator iterator()
This method return a java.util.Iterator of every registered stores The iterators returned is fail-fast: if list is structurally modified at any time after the iterator is created, in any way, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Specified by:
iterator in interface StoreJanitor
Returns:
a java.util.Iterator

freeMemory

private void freeMemory()
Round Robin alghorithm for freeing the registered caches.


calcToFree

private int calcToFree(Store store)
This method claculates the number of Elements to be freememory out of the Cache.

Parameters:
store - the Store which was selected as victim
Returns:
number of elements to be removed!

freePhysicalMemory

private void freePhysicalMemory()
This method forces the garbage collector


getMinFreeMemory

private int getMinFreeMemory()

getMaxHeapSize

private int getMaxHeapSize()

getPriority

private int getPriority()

getThreadInterval

private int getThreadInterval()

getAdaptiveThreadInterval

private boolean getAdaptiveThreadInterval()

getJVM

private java.lang.Runtime getJVM()

getStoreList

private java.util.ArrayList getStoreList()

setIndex

private void setIndex(int _index)

getIndex

private int getIndex()