Class LowMemory


  • public class LowMemory
    extends java.lang.Object
    Methods to aid classes recover from OutOfMemoryErrors by denying or reducing service rather than a complete shutdown of the JVM. It's intended that classes use to functionality to allow then to deny service when memory is low to allow the JVM to recover, rather than start new operations that are probably doomed to failure due to the low memory.

    Expected usage is one instance of this class per major logical operation, e.g. creating a connection, preparing a statement, adding an entry to a specific cache etc.
    The logical operation would call isLowMemory() before starting the operation, and thrown a static exception if it returns true.
    If during the operation an OutOfMemoryException is thrown the operation would call setLowMemory() and throw its static exception representing low memory.

    Future enhancments could be a callback mechanism for modules where they register they can reduce memory usage on a low memory situation. These callbacks would be triggered by a call to setLowMemory. For example the page cache could reduce its current size by 10% in a low memory situation.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long lowMemory
      Free memory seen when caller indicated an out of memory situation.
      private long whenLowMemorySet
      Time in ms corresponding to System.currentTimeMillis() when lowMemory was set.
    • Constructor Summary

      Constructors 
      Constructor Description
      LowMemory()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isLowMemory()
      Return true if a low memory water mark has been set and the current free memory is lower than it.
      void setLowMemory()
      Set a low memory watermark where the owner of this object just hit an OutOfMemoryError.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lowMemory

        private long lowMemory
        Free memory seen when caller indicated an out of memory situation. Becomes a low memory watermark for five seconds that causes isLowMemory to return true if free memory is lower than this value. This allows the JVM a chance to recover memory rather than start new operations that are probably doomed to failure due to the low memory.
      • whenLowMemorySet

        private long whenLowMemorySet
        Time in ms corresponding to System.currentTimeMillis() when lowMemory was set.
    • Constructor Detail

      • LowMemory

        public LowMemory()
    • Method Detail

      • setLowMemory

        public void setLowMemory()
        Set a low memory watermark where the owner of this object just hit an OutOfMemoryError. The caller is assumed it has just freed up any references it obtained during the operation, so that the freeMemory call as best as it can reflects the memory before the action that caused the OutOfMemoryError, not part way through the action.
      • isLowMemory

        public boolean isLowMemory()
        Return true if a low memory water mark has been set and the current free memory is lower than it. Otherwise return false.