org.icepdf.core.util
Class MemoryManager

java.lang.Object
  extended by org.icepdf.core.util.MemoryManager

public class MemoryManager
extends java.lang.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  java.util.ArrayList<MemoryManagerDelegate> delegates
           
protected  java.util.ArrayList<MemoryManageable> leastRecentlyUsed
           
protected  java.util.WeakHashMap<java.lang.Object,java.util.HashSet<MemoryManageable>> 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  java.lang.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 getMinMemory()
          Get the minimum amount of memory
protected  boolean isLocked(MemoryManageable mm)
           
 boolean isLowMemory()
          Check whether the runtime is low on memory.
 void lock(java.lang.Object user, MemoryManageable mm)
           
protected  boolean reduceMemory()
           
protected  int reduceMemory(int numToDo)
           
protected  boolean reduceMemoryWithDelegates(boolean aggressively)
           
 void registerMemoryManagerDelegate(MemoryManagerDelegate delegate)
           
 void release(java.lang.Object user, MemoryManageable mm)
           
 void releaseAllByLibrary(Library library)
           
 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 java.lang.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 org.icepdf.core.minMemory


maxMemory

protected long maxMemory
The maximum amount of memory allocated to the JVM.


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 java.util.WeakHashMap<java.lang.Object,java.util.HashSet<MemoryManageable>> locked

leastRecentlyUsed

protected java.util.ArrayList<MemoryManageable> leastRecentlyUsed

cumulativeDurationManagingMemory

protected long cumulativeDurationManagingMemory

cumulativeDurationNotManagingMemory

protected long cumulativeDurationNotManagingMemory

previousTimestampManagedMemory

protected long previousTimestampManagedMemory

percentageDurationManagingMemory

protected int percentageDurationManagingMemory

delegates

protected java.util.ArrayList<MemoryManagerDelegate> delegates
Constructor Detail

MemoryManager

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

Method Detail

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 org.icepdf.core.minMemory

lock

public void lock(java.lang.Object user,
                 MemoryManageable mm)

release

public void release(java.lang.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 core will go into the memory recovery mode.

Parameters:
m - minimum amount of memory that should be kept free on the heap

getMinMemory

public long getMinMemory()
Get the minimum amount of memory

Returns:
minimum amount of memory that should be kept free on the 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. Page initialization and other large memory allocation operations should call this method before (during) attempts to allocate resources.

Returns:
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 - memory looking to allocate
Returns:
true if it's sure we can allocate memoryNeeded number of bytes