org.hypergraphdb.util
Class MemoryWarningSystem

java.lang.Object
  extended by org.hypergraphdb.util.MemoryWarningSystem

public class MemoryWarningSystem
extends java.lang.Object

This memory warning system will call all registered listeners when we exceed the percentage of available heap memory specified.

There should only be one instance of this object created, since the usage threshold can only be set to one number. A HyperGraphDB JVM-wide singleton is available statically from HGEnvironment class.

HyperGraph will configure a default usage threshold percentage for the HEAP (i.e. "tenure generation") JVM memory pool to 0.9. This means that listeners will be invoked when used memory reaches about 90% of the maximum available memory. You can change that percentage by calling the setPercentageUsage method and that will globally affect the behavior of all running code. This is an unfortunate design of the JVM - it doesn't allow more than threshold to be configured. It's possible for one to write one's own thread that monitor heap usage, but this begs the question whether the overhead is worth it.

NOTE: The goal of this is clearly to reduce memory consumption in caches and the like. Because each listener will shrink memory in an independent way, the result of invoking all of them will perhaps lead to an overzealous cleanup of useful cached information. In the future, it would be a good idea to architect some sort of cooperation of such cleanup code. Perhaps a desired threshold could be set and each listener invoked iteratively to perform "a little bit of cleanup" as many times as needed to reach that threshold.

Code taken from http://www.roseindia.net/javatutorials/OutOfMemoryError_Warning_System.shtml


Nested Class Summary
static interface MemoryWarningSystem.Listener
           
 
Constructor Summary
MemoryWarningSystem()
           
 
Method Summary
 boolean addListener(MemoryWarningSystem.Listener listener)
           
 double getPercentageUsageThreshold()
           
 boolean removeListener(MemoryWarningSystem.Listener listener)
           
 void setPercentageUsageThreshold(double percentage)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MemoryWarningSystem

public MemoryWarningSystem()
Method Detail

addListener

public boolean addListener(MemoryWarningSystem.Listener listener)

removeListener

public boolean removeListener(MemoryWarningSystem.Listener listener)

setPercentageUsageThreshold

public void setPercentageUsageThreshold(double percentage)

getPercentageUsageThreshold

public double getPercentageUsageThreshold()