cleanup action captures original segment

Wojciech Lukowicz wlukowicz01 at gmail.com
Sun Dec 29 10:50:44 UTC 2024


Hi,

I wanted to verify some automatic arena gets cleaned up and wrote
something equivalent to:

var arena = Arena.ofAuto();
arena.allocate(0).reinterpret(arena, _ -> System.out.println("cleanup"));
arena = null;

However, after many GC cycles, the cleanup action never got executed.

Turns out AbstractMemorySegmentImpl#reinterpretInternal creates a lambda
wrapper around the cleanup action which captures the original segment
and consequently its scope, doing here what the JavaDoc warns against:

> Furthermore, if the provided scope is the scope of an automatic arena,
> the cleanup action must not prevent the scope from becoming unreachable.
> A failure to do so will permanently prevent the regions of memory
> allocated by the automatic arena from being deallocated.

Not an issue in the typical case of reinterpreting global segments, but
the behavior seems accidental.

Thanks,
Wojciech


More information about the panama-dev mailing list