[foreign-preview] RFR: 8276648: Downcall stubs are never freed

Jorn Vernee jvernee at openjdk.java.net
Tue Feb 22 15:26:00 UTC 2022


On Tue, 22 Feb 2022 14:47:35 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Hi,
>> 
>> The current code adds native invokers (downcall stubs) to a CocurrentHashMap cache, and they stay there until the application exits.
>> 
>> This patch replaces that cache with a SoftReferenceCache (brought to the top level from AbstractLinker), which allows at least the values to be evicted when they become unreachable.
>> 
>> Those value, which are NativeEntryPoint instances, are also registered with a cleaner, whose cleanup action will free the stub from the code cache.
>> 
>> Thanks,
>> Jorn
>
> src/java.base/share/classes/jdk/internal/foreign/abi/NativeEntryPoint.java line 68:
> 
>> 66: 
>> 67:         CacheKey key = new CacheKey(methodType, abi, Arrays.asList(argMoves), Arrays.asList(returnMoves), needsReturnBuffer);
>> 68:         return INVOKER_CACHE.get(key, k -> {
> 
> This uses a combination of soft references (cache) and phantom references (cleaner). So, cache entries will be evicted when the NEP is softly reachable - meaning that the GC will clear the entries when under pressure. But the cleaner won't be able to run until the entry is evicted (because the entry has a soft reference to the NEP, and to be phantom-reachable you need NOT to be soft-reachable). Assuming that's what you want, then it looks ok. E.g. the cleaner here will "just" make sure that some action is executed when the entry is evicted.

Yes, that is the intent. We want to keep the downcall stub around until after the NEP is evicted from the cache. The cleanup is just there to make sure the VM stub is freed as well, and it is tied to the reachability of the associated NEP, which is strongly reachable from a downcall method handle.

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/651


More information about the panama-dev mailing list