MemoryManager.allocatePages uses this::releasePage as cleanup action in MemorySegmentFactory.allocateOffHeapUnsafeMemory. The cleanup function is used as gc cleaner action there. This creates a cycle referencing between memory manager and gc cleaner if allocated memory segment is not MemoryManager.release in time. Symptoms should be different based on versions:
- Before 1.12.2: memory will not be reclaimed until gc after MemoryManager.release
- * 1.12.2: memory will not be reclaimed until MemorySegment.free or gc after MemoryManager.release
I quotes javadoc from jdk java.lang.ref.Cleaner here for references:
The cleaning action is invoked only after the associated object becomes phantom reachable, so it is important that the object implementing the cleaning action does not hold references to the object. In this example, a static class encapsulates the cleaning state and action. An "inner" class, anonymous or not, must not be used because it implicitly contains a reference to the outer instance, preventing it from becoming phantom reachable. The choice of a new cleaner or sharing an existing cleaner is determined by the use case.