Class AllocationMonitor
- java.lang.Object
-
- it.unich.jgmp.AllocationMonitor
-
public class AllocationMonitor extends java.lang.ObjectThe 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_functionsandmp_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 theenable()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,lowerThresholdandmaxTimeout. 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 themaxTimeouttunable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAllocationMonitor.JGMPAllocThe custom allocator function.private static classAllocationMonitor.JGMPFreeThe custom deallocator.private static classAllocationMonitor.JGMPReallocThe custom reallocator.
-
Field Summary
Fields Modifier and Type Field Description private static AllocFuncafprivate static AllocFuncByReferenceafpOldprivate static java.util.concurrent.atomic.AtomicLongallocatedSizeThe amount of native memory allocated by GMP, as recorded by the allocation monitor.private static longallocationThresholdThe allocation threshold.private static intdebugLevelThe debug level of the allocation monitor.private static FreeFuncffprivate static FreeFuncByReferenceffpOldprivate static intgcCallsKeep track of the number of times that GC has been called by the allocation monitor.private static longlowerThresholdThe lower threshold.private static longmaxAllocatedSizeThe maximum amount of memory allocated by GMP.private static intmaxStepTimeoutThe maximum delay for a single timeout step.private static ReallocFuncrfprivate static ReallocFuncByReferencerfpOldprivate static intstepTimeoutThe current delay for a single timeout step.private static intTIMEOUT_STEPSNumber of steps in which we divide the timeout interval.
-
Constructor Summary
Constructors Modifier Constructor Description privateAllocationMonitor()A private constructor, since this class should never be instantiated.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static voidcheckGC(long increase)Check if the garbage collector needs to be invoked, and update the numCrossed and allocation thresholds.private static voiddebugInfo()Print debugging information.static voiddisable()Disable the allocation monitor.static voidenable()Enable the allocation monitor.static longgetAllocatedSize()Return the amount of native memory allocated by JGMP, as recorded by the allocation monitor.static longgetAllocationThreshold()Return the current allocation threshold.static intgetDebugLevel()Return the current debug level of the allocation monitor.static intgetGcCalls()Return the number of times that GC has been called by the allocation monitor.static longgetLowerThreshold()Return the current value of the lower threshold.static longgetMaxAllocatedSize()Return the maximum amount of memory ever allocated by the JGMP at the same moment, as recorded by the allocation monitor.static intgetTimeout()Return the current timeout value.static voidsetAllocationThreshold(long value)Set the current allocation threshold.static voidsetDebugLevel(int debugLevel)Set the debug level of the allocation monitor.static voidsetLowerThreshold(long value)Set the current value of the lower threshold.static voidsetTimeout(int value)Set the maximum timeout value.
-
-
-
Field Detail
-
debugLevel
private static volatile int debugLevel
The debug level of the allocation monitor.
-
allocatedSize
private static java.util.concurrent.atomic.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:
- Constant Field Values
-
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
-
-
Method Detail
-
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.
-
-