Class MemoryUtils

java.lang.Object
uk.ac.ebi.utils.memory.MemoryUtils

public class MemoryUtils extends Object
An helper that allows to release/reset resources when free memory goes beyond a given limit. This is needed in certain cases, eg, when it's not clear why Hibernate keep filling up the memory and it has been proved that reinitialising the enity manager fixes the problem.
date
Jul 11, 2011
Author:
brandizi
  • Method Details

    • checkMemory

      public 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). 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

      public static boolean checkMemory(Runnable action, double minFreeMemory)
      Defaults to callGc = true.
    • checkMemory

      public static boolean checkMemory(Runnable action)
      Defaults to 0.2 (20%) and true.
    • registerCleaner

      public static Cleaner.Cleanable registerCleaner(Object obj, Runnable cleanAction)
      Simple wrapper for the new J9 Cleaner interface, which replaces the old finalize() method.
      Parameters:
      cleanAction - is the old finalize method, something you want to do when the object isn't reachable anymore. This is registered into a new cleaner and hence set to be auto-invoked when necessary.
      See Also: