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(
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(
public static < | Posts the object for trimming using the trimmer function. |
public static void | trimAll() Trims all the objects in the queue. |
public static void | trimInterruptible( Trims the objects in the queue with optional interruptability. |
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.
The trimming will stop if either no more objects remaining or the predicate returns true
.