saker.build Documentation TaskDoc JavaDoc Packages
public class MemoryTrimmer
Static class for allowing clients to trim memory of objects when appropriate.

MemoryTrimmer serves as JVM-wide utility class to collect objects and trim their memory allocation when an appropriate time is found to do it. This time is usually after a build execution is finished.

Clients can use the add(T, Consumer<super T>) function to post objects for trimming later.

All of the posted objects in this class are weakly referenced, except the trimmer functions. This allows premature garbage collection of the trimmable objects. The trimmer functions should not strongly reference the trimmable object, it will be passed to it as an argument when trimming occurs.

Trimming can be invoked manually by anyone using the functions starting with trim. The class ensures that a trimming function for a single object will be only called at most once for each add(T, Consumer<super T>) calls with it.

Methods
public static <T> void
add(T object, Consumer<super T> trimmerfunction)
Posts the object for trimming using the trimmer function.
public static void
Trims all the objects in the queue.
public static void
trimInterruptible(BooleanSupplier shouldinterruptpredicate)
Trims the objects in the queue with optional interruptability.
public static <T> void add(T object, Consumer<super T> trimmerfunction)
Posts the object for trimming using the trimmer function.

The trimmer function must not hold a strong reference to the object. The object will be passed to the trimmer function at the time of trimming.

Callers can expect the object to be trimmed in any time after initiating this method call. However, they must not expect that the trimming will always or even deterministicly occur.

objectThe object to trim.
trimmerfunctionThe trimmer function.
public static void trimAll()
Trims all the objects in the queue.
public static void trimInterruptible(BooleanSupplier shouldinterruptpredicate)
Trims the objects in the queue with optional interruptability.

The trimming will stop if either no more objects remaining or the predicate returns true.

shouldinterruptpredicateThe interrupt predicate.