com.icesoft.pdf.util
Class MemoryManager

java.lang.Object
  extended bycom.icesoft.pdf.util.MemoryManager

public class MemoryManager
extends Object

The MemoryManager class is a utility to help manage the amount of memory available to the application. When memory intensive operations are about occur, the MemoryManager is asked if it can allocate the needed amount of memory. If there is not enough memory available, the MemoryManager will purge the cache to try and free the requested amount of memory.


Field Summary
protected  long cumulativeDurationManagingMemory
           
protected  long cumulativeDurationNotManagingMemory
           
protected  ArrayList delegates
           
protected  ArrayList leastRecentlyUsed
           
protected  WeakHashMap locked
           
protected  long maxMemory
          The maximum amount of memory allocated to the JVM.
protected  int maxSize
          If a memory-based ceiling, like maxMemory, is not sufficient, then you can use maxSize to specify the maximum number of items that may be opened before purging commences.
protected  long minMemory
          The minimum amount of free memory at which the memory manager will force a purge of the cached pageTree.
protected  int percentageDurationManagingMemory
           
protected  long previousTimestampManagedMemory
           
protected  int purgeSize
          When we decide to reduce our memory footprint, this is how many items we purge at once.
protected  Runtime runtime
          Runtime object responsible for returning VM memory use information
 
Constructor Summary
protected MemoryManager()
          Creates a new instance of a MemoryManager.
 
Method Summary
 boolean checkMemory(int memoryNeeded)
           
 long getFreeMemory()
          Get runtime free memory.
static MemoryManager getInstance()
          Get an instance of the MemoryManager.
 long getMaxMemory()
          Get maximum amount of jvm heap.
 long getMinMemory()
          Get the minimum amount of memory
protected  boolean isLocked(MemoryManageable mm)
           
 boolean isLowMemory()
          Check whether the runtime is low on memory.
 void lock(Object user, MemoryManageable mm)
           
protected  boolean reduceMemory()
           
protected  int reduceMemory(int numToDo)
           
protected  boolean reduceMemoryWithDelegates(boolean aggressively)
           
 void registerMemoryManagerDelegate(MemoryManagerDelegate delegate)
           
 void release(Object user, MemoryManageable mm)
           
 void releaseAllByLibrary(Library library)
           
static void setInstance(MemoryManager memoryMangaer)
          Sets an existing MemoryManager object to this MemoryManager object.
 void setMaxMemory(long m)
          Set the maximum amount of JVM heap.
 void setMinMemory(long m)
          Set the minimum amount of memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

runtime

protected final Runtime runtime
Runtime object responsible for returning VM memory use information


minMemory

protected long minMemory
The minimum amount of free memory at which the memory manager will force a purge of the cached pageTree. This value can be set by the system property com.icesoft.pdf.minMemory


maxMemory

protected long maxMemory
The maximum amount of memory allocated to the JVM. There is a new method in JDK 1.4 getMaxMemory which will return this value automatically, but is not available in JDK 1.3. This value should be set to the same value of the system property -Xmx. The default value of 63MB can be set using the system property com.icesoft.pdf.maxMemory


purgeSize

protected int purgeSize
When we decide to reduce our memory footprint, this is how many items we purge at once.


maxSize

protected int maxSize
If a memory-based ceiling, like maxMemory, is not sufficient, then you can use maxSize to specify the maximum number of items that may be opened before purging commences. A value of 0 (zero) means it will not be used


locked

protected WeakHashMap locked

leastRecentlyUsed

protected ArrayList leastRecentlyUsed

cumulativeDurationManagingMemory

protected long cumulativeDurationManagingMemory

cumulativeDurationNotManagingMemory

protected long cumulativeDurationNotManagingMemory

previousTimestampManagedMemory

protected long previousTimestampManagedMemory

percentageDurationManagingMemory

protected int percentageDurationManagingMemory

delegates

protected ArrayList delegates
Constructor Detail

MemoryManager

protected MemoryManager()
Creates a new instance of a MemoryManager.

Method Detail

setInstance

public static void setInstance(MemoryManager memoryMangaer)
Sets an existing MemoryManager object to this MemoryManager object.

Parameters:
memoryMangaer - MemoryManager object to point to.

getInstance

public static MemoryManager getInstance()
Get an instance of the MemoryManager. If there is not a MemoryManager initiated, a new MemoryManager is is created and returned.

Returns:
the current MemoryManager object com.icesoft.pdf.minMemory

lock

public void lock(Object user,
                 MemoryManageable mm)

release

public void release(Object user,
                    MemoryManageable mm)

registerMemoryManagerDelegate

public void registerMemoryManagerDelegate(MemoryManagerDelegate delegate)

releaseAllByLibrary

public void releaseAllByLibrary(Library library)

reduceMemory

protected boolean reduceMemory()
Returns:
If potentially reduced some memory

reduceMemory

protected int reduceMemory(int numToDo)

isLocked

protected boolean isLocked(MemoryManageable mm)

reduceMemoryWithDelegates

protected boolean reduceMemoryWithDelegates(boolean aggressively)

setMinMemory

public void setMinMemory(long m)
Set the minimum amount of memory. Basically, if the amount of free heap is under this value, the browser will go into the error recovery mode.


getMinMemory

public long getMinMemory()
Get the minimum amount of memory


setMaxMemory

public void setMaxMemory(long m)
Set the maximum amount of JVM heap. This should be the same value as you give to jvm (normally with -mx parameter). The MemoryManager will use this number to determine whether it is low on memory. The manager also uses Runtime.freeMemory() call but that call returns the free memory with only the current heap size, not the maximum heap size.


getMaxMemory

public long getMaxMemory()
Get maximum amount of jvm heap.


getFreeMemory

public long getFreeMemory()
Get runtime free memory.

Returns:
free memory in bytes.

isLowMemory

public boolean isLowMemory()
Check whether the runtime is low on memory. All well behaved browser components (pilots and scripters) should call this method before (during) attempts to allocate resources. If this method returns true, such a component should stop its operation and free up the memory it has allocated.


checkMemory

public boolean checkMemory(int memoryNeeded)
Parameters:
memoryNeeded -
Returns:
true if it's sure we can allocate memoryNeeded number of bytes