Package it.unich.jgmp

Class AllocationMonitor

java.lang.Object
it.unich.jgmp.AllocationMonitor

public class AllocationMonitor extends Object
The allocation monitor keeps track of the amount of native memory allocated by GMP, and calls the Java garbage collector when it finds that too much native memory is being used. The hope is that, by destroying JGMP objects, the pressure on native memory is reduced.

In order to keep track of allocated native memory, this class uses the GMP fuctions mp_set_memory_functions and mp_get_memory_functions (see the Custom Allocation page of the GMP manual). Since this slows down allocation, the feature is normally disabled and may be enable by calling the enable() static method.

It is important to enable allocation monitor when a program builds many big JGMP objects. In this case, since the size occupied by a JGMP object in the Java heap is only a fraction of the size occupied in native memory, the program may consume all the native memory without the JVM feeling the need to call the garbage collector to reclaim heap space. This may happen, in particular, when making use of the immutable API.

The current allocator has three tunables: allocationThreshold, lowerThreshold and maxTimeout. Every allocation or reallocation of native memory by the GMP library makes the allocated size larger than the allocation threshold, causes a call to the Java garabage collector. Then, we wait until the allocated memory falls below the lower threshold, or until a timeout has expired. The length of the timeout is dinamically computed by the allocation monitor, but it never exceed the value of the maxTimeout tunable.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    The custom allocator function.
    private static class 
    The custom deallocator.
    private static class 
    The custom reallocator.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static AllocFunc
     
    private static AllocFuncByReference
     
    private static AtomicLong
    The amount of native memory allocated by GMP, as recorded by the allocation monitor.
    private static long
    The allocation threshold.
    private static int
    The debug level of the allocation monitor.
    private static FreeFunc
     
    private static FreeFuncByReference
     
    private static int
    Keep track of the number of times that GC has been called by the allocation monitor.
    private static long
    The lower threshold.
    private static long
    The maximum amount of memory allocated by GMP.
    private static int
    The maximum delay for a single timeout step.
    private static ReallocFunc
     
    private static ReallocFuncByReference
     
    private static int
    The current delay for a single timeout step.
    private static final int
    Number of steps in which we divide the timeout interval.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    A private constructor, since this class should never be instantiated.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static void
    checkGC(long increase)
    Check if the garbage collector needs to be invoked, and update the numCrossed and allocation thresholds.
    private static void
    Print debugging information.
    static void
    Disable the allocation monitor.
    static void
    Enable the allocation monitor.
    static long
    Return the amount of native memory allocated by JGMP, as recorded by the allocation monitor.
    static long
    Return the current allocation threshold.
    static int
    Return the current debug level of the allocation monitor.
    static int
    Return the number of times that GC has been called by the allocation monitor.
    static long
    Return the current value of the lower threshold.
    static long
    Return the maximum amount of memory ever allocated by the JGMP at the same moment, as recorded by the allocation monitor.
    static int
    Return the current timeout value.
    static void
    Set the current allocation threshold.
    static void
    setDebugLevel(int debugLevel)
    Set the debug level of the allocation monitor.
    static void
    setLowerThreshold(long value)
    Set the current value of the lower threshold.
    static void
    setTimeout(int value)
    Set the maximum timeout value.

    Methods inherited from class java.lang.Object

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

    • debugLevel

      private static volatile int debugLevel
      The debug level of the allocation monitor.
    • allocatedSize

      private static AtomicLong allocatedSize
      The amount of native memory allocated by GMP, as recorded by the allocation monitor. It is an `AtomicLong` since it might be increased concurrently by multple threads.
    • allocationThreshold

      private static volatile long allocationThreshold
      The allocation threshold.
    • lowerThreshold

      private static volatile long lowerThreshold
      The lower threshold.
    • TIMEOUT_STEPS

      private static final int TIMEOUT_STEPS
      Number of steps in which we divide the timeout interval.
      See Also:
    • maxStepTimeout

      private static volatile int maxStepTimeout
      The maximum delay for a single timeout step.
    • stepTimeout

      private static volatile int stepTimeout
      The current delay for a single timeout step.
    • gcCalls

      private static volatile int gcCalls
      Keep track of the number of times that GC has been called by the allocation monitor.
    • maxAllocatedSize

      private static volatile long maxAllocatedSize
      The maximum amount of memory allocated by GMP.
    • af

      private static AllocFunc af
    • rf

      private static ReallocFunc rf
    • ff

      private static FreeFunc ff
    • afpOld

      private static AllocFuncByReference afpOld
    • rfpOld

      private static ReallocFuncByReference rfpOld
    • ffpOld

      private static FreeFuncByReference ffpOld
  • Constructor Details

    • AllocationMonitor

      private AllocationMonitor()
      A private constructor, since this class should never be instantiated.
  • Method Details

    • setDebugLevel

      public static void setDebugLevel(int debugLevel)
      Set the debug level of the allocation monitor. The greater the value, the more debug messages are sent to the standard error. Zero and negative numbers mean that no debug messages are generated.
    • getDebugLevel

      public static int getDebugLevel()
      Return the current debug level of the allocation monitor.
    • getAllocatedSize

      public static long getAllocatedSize()
      Return the amount of native memory allocated by JGMP, as recorded by the allocation monitor.
    • getAllocationThreshold

      public static long getAllocationThreshold()
      Return the current allocation threshold.
    • setAllocationThreshold

      public static void setAllocationThreshold(long value)
      Set the current allocation threshold. This method also sets the default value for the lower threshold, which is 15/16 of the allocation threshold.
    • getLowerThreshold

      public static long getLowerThreshold()
      Return the current value of the lower threshold.
    • setLowerThreshold

      public static void setLowerThreshold(long value)
      Set the current value of the lower threshold.
    • setTimeout

      public static void setTimeout(int value)
      Set the maximum timeout value. The default value is 200 ms.
    • getTimeout

      public static int getTimeout()
      Return the current timeout value.
    • getGcCalls

      public static int getGcCalls()
      Return the number of times that GC has been called by the allocation monitor.
    • getMaxAllocatedSize

      public static long getMaxAllocatedSize()
      Return the maximum amount of memory ever allocated by the JGMP at the same moment, as recorded by the allocation monitor.
    • debugInfo

      private static void debugInfo()
      Print debugging information.
    • checkGC

      static void checkGC(long increase)
      Check if the garbage collector needs to be invoked, and update the numCrossed and allocation thresholds.
    • enable

      public static void enable()
      Enable the allocation monitor. Nothing happens if the monitor is already enabled.
    • disable

      public static void disable()
      Disable the allocation monitor. Nothing happens if the monitor is already disabled.