Class MemoryUtils
- date
- Jul 11, 2011
- Author:
- brandizi
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkMemory
(Runnable action) Defaults to 0.2 (20%) and true.static boolean
checkMemory
(Runnable action, double minFreeMemory) Defaults to callGc = true.static boolean
checkMemory
(Runnable action, double minFreeMemoryRatio, boolean callGc) Invoke this when you think it's safe to invoke the action (eg, when you can get rid of all the objects and connections in an Hibernate entity manager).static Cleaner.Cleanable
registerCleaner
(Object obj, Runnable cleanAction) Simple wrapper for the new J9Cleaner
interface, which replaces the oldfinalize()
method.
-
Method Details
-
checkMemory
Invoke this when you think it's safe to invoke the action (eg, when you can get rid of all the objects and connections in an Hibernate entity manager). The method checks the amount of free memory still available to the JVM and, if this is < minFreeMemoryRatio, executes the action and then, if callGc is true, invokes the
garbage collector
.The event triggering is also logged with trace level.
The amount of free memory is computed as:
- runtime.maxMemory () - ( runtime.totalMemory () - runtime.freeMemory () )
That's because the JVM allocates more memory, up to maxMemory(), when freeMemory() reaches the totalMemory() amount. (hence the above formula first computes the occupied memory and then the true free one
Note the method is synchronised, so that you can avoid that a number of threads trigger a memory reset almost simultaneously.
-
checkMemory
Defaults to callGc = true. -
checkMemory
Defaults to 0.2 (20%) and true. -
registerCleaner
Simple wrapper for the new J9Cleaner
interface, which replaces the oldfinalize()
method.- Parameters:
cleanAction
- is the old finalize method, something you want to do when the object isn't reachable anymore. This isregistered
into a new cleaner and hence set to be auto-invoked when necessary.- See Also:
-