Class AllocationMonitor
- java.lang.Object
-
- it.unich.jgmp.AllocationMonitor
-
public class AllocationMonitor extends java.lang.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
andmp_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
,lowerThreshold
andmaxTimeout
. 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 themaxTimeout
tunable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
AllocationMonitor.JGMPAlloc
The custom allocator function.private static class
AllocationMonitor.JGMPFree
The custom deallocator.private static class
AllocationMonitor.JGMPRealloc
The custom reallocator.
-
Field Summary
Fields Modifier and Type Field Description private static AllocFunc
af
private static AllocFuncByReference
afpOld
private static java.util.concurrent.atomic.AtomicLong
allocatedSize
The amount of native memory allocated by GMP, as recorded by the allocation monitor.private static long
allocationThreshold
The allocation threshold.private static int
debugLevel
The debug level of the allocation monitor.private static FreeFunc
ff
private static FreeFuncByReference
ffpOld
private static int
gcCalls
Keep track of the number of times that GC has been called by the allocation monitor.private static long
lowerThreshold
The lower threshold.private static long
maxAllocatedSize
The maximum amount of memory allocated by GMP.private static int
maxStepTimeout
The maximum delay for a single timeout step.private static ReallocFunc
rf
private static ReallocFuncByReference
rfpOld
private static int
stepTimeout
The current delay for a single timeout step.private static int
TIMEOUT_STEPS
Number of steps in which we divide the timeout interval.
-
Constructor Summary
Constructors Modifier Constructor Description private
AllocationMonitor()
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 void
checkGC(long increase)
Check if the garbage collector needs to be invoked, and update the numCrossed and allocation thresholds.private static void
debugInfo()
Print debugging information.static void
disable()
Disable the allocation monitor.static void
enable()
Enable the allocation monitor.static long
getAllocatedSize()
Return the amount of native memory allocated by JGMP, as recorded by the allocation monitor.static long
getAllocationThreshold()
Return the current allocation threshold.static int
getDebugLevel()
Return the current debug level of the allocation monitor.static int
getGcCalls()
Return the number of times that GC has been called by the allocation monitor.static long
getLowerThreshold()
Return the current value of the lower threshold.static long
getMaxAllocatedSize()
Return the maximum amount of memory ever allocated by the JGMP at the same moment, as recorded by the allocation monitor.static int
getTimeout()
Return the current timeout value.static void
setAllocationThreshold(long value)
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.
-
-
-
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.
-
-