Package ej.bon

Class Immortals


  • public class Immortals
    extends Object
    This class gives access to the global immortal objects pool.

    An immortal object has two major properties:

    1. it is not managed by the garbage collector,
    2. it does not move around in memory, i.e. its physical memory location remains the same forever.
    • Constructor Detail

      • Immortals

        public Immortals()
    • Method Detail

      • isImmortal

        public static boolean isImmortal​(Object o)
        Gets whether an object is immortal or not.

        An object is immortal:

        Parameters:
        o - the object to check
        Returns:
        true if the queried object is immortal or immutable, false otherwise
        Throws:
        NullPointerException - if given object is null
        See Also:
        Immutables.isImmutable(Object)
      • setImmortal

        public static <T> T setImmortal​(T o)
        Turns the given object into an immortal object.

        If the received object was immutable, it remains immutable.

        If the object was already immortal, it remains immortal.

        If the object was a reclaimable object it turns into an immortal object. Upon success, the returned object is immortal, otherwise an OutOfMemoryError is thrown.

        Type Parameters:
        T - the object type
        Parameters:
        o - the object to turn into an immortal
        Returns:
        the given object turned into immortal
        Throws:
        OutOfMemoryError - if the immortal memory is full
      • deepImmortal

        public static <T> T deepImmortal​(T root)
        Turns the given object and all objects referred from it into immortal objects.

        Weakly reachable objects are not turned into immortal objects.

        Type Parameters:
        T - the root object type
        Parameters:
        root - the root of the objects graph to turn into immortal
        Returns:
        the given object turned into immortal
      • run

        public static void run​(Runnable runnable)
        Calls the method Runnable.run() of the given runnable.

        All the objects allocated in the context of this method are directly allocated as immortals. While the Runnable.run() method of the runnable executes all created objects are allocated as immortal objects.

        Parameters:
        runnable - the runnable to execute
        Throws:
        NullPointerException - if the given runnable is null
      • freeMemory

        public static long freeMemory()
        Returns the amount of free immortal memory still available.
        Returns:
        the amount of free immortal memory
      • totalMemory

        public static long totalMemory()
        Returns the total amount of immortal memory.

        Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

        Returns:
        the total amount of immortal memory