ice.util.memory
Class MemoryManager

java.lang.Object
  extended byice.util.memory.MemoryManager
Direct Known Subclasses:
VerboseMemoryManager

public class MemoryManager
extends Object

This object keeps track of current memory usage. It is very important that each well-behaving pilot calls isLowMemory() method of this class before allocating substantial amount of memory. And if the method returns true - the pilot should quite as fast as possible cleaning up the resources it has been using.

This approach is much better than just waiting for OutOfMemory exception to happen. Since when such an exception happens it is usually too late to do anything as the system is really out of memory.

An application can ovverride this implementation to add more robust memory recovery. So for example when the system is low on memory the application can try to close other applications etc.

To ensure the proper usage of the memory manager in the browser, the application MUST specify the absolute maximum amount of memory on java heap. Normally this value is given to the JVM with '-mx' option. Since there is no way to extract that value from JVM the browser should be given that value. You can use either the API calls or system properties: ice.browser.minMemory and ice.browser.maxMemory.


Field Summary
protected  long maxMemory
           
protected  long minMemory
           
protected  Runtime runtime
           
 
Constructor Summary
protected MemoryManager()
           
 
Method Summary
 boolean canAllocate(int bytes)
          Will return true if the system is not in a low memory condition after allocation of specified number of bytes.
static MemoryManager getInstance()
           
 long getMaxMemory()
          Get maximum amount of jvm heap.
 long getMinMemory()
          Get the minimum amount of memory
 boolean isLowMemory()
          Check whether the runtime is low on memory.
static void setInstance(MemoryManager mem)
           
 void setMaxMemory(long m)
          Set the maximum amount of jvm heap.
 void setMinMemory(long m)
          Set the minimum amout 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

minMemory

protected long minMemory

maxMemory

protected long maxMemory
Constructor Detail

MemoryManager

protected MemoryManager()
Method Detail

setInstance

public static void setInstance(MemoryManager mem)

getInstance

public static MemoryManager getInstance()

setMinMemory

public void setMinMemory(long m)
Set the minimum amout 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 memory manager will use this number to detrmine whether it is low on memory. The manager also uses Runtime.freeMemory() call but that call returns only the free memory with the current heap size, not with the maximum heap size.


getMaxMemory

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


canAllocate

public boolean canAllocate(int bytes)
Will return true if the system is not in a low memory condition after allocation of specified number of 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.