java-native-access / java-native-access/jna
FFITypes for non-base classes are never removed from `typeInfoMap`, causing the Cleaner thread to loop forever
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 8.9k
- Forks
- 1.7k
- PR merge metrics
- No merged PRs in 30d
Description
The FFIType class has a static WeakHashMap of type information, the typeInfoMap. Since it is a weak map, key-value pairs would be removed if the key was garbage collected. However, since the keys in this map are Classes, they are extremely unlikely to ever be collected.
For classes that are Structures, the FFIType stored in the typeInfoMap wraps the structure object itself, here. However, when the FFIType is a Structure, there is underlying Memory in it, which has a MemoryDisposer registered on it.
Since the Class is never unloaded, the FFType cannot be garbage collected either -- however, the Cleaner thread expects to be able to clean up its Memory, and so it will loop forever waiting to be able to clean it.
Potential solutions: I am not sure the best way to go about fixing this problem, but there are two approaches I think of first:
- Change the way FFITypes for Structures are made, so they don't have any underlying Memory.
- Use a different, special kind of Memory for FFITypes, that has no cleaner registration. Since this memory corresponds to a Class, I think it could be safe to assume there are a limited number of classes being loaded.
Let me know what you think. I''m trying to hack around this problem right now in my project, and will update this issue when I have a temporary solution too.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Structure.java at FFIType and typeInfoMap, then trace the Structure FFIType construction around the referenced line. Read Memory.java around MemoryDisposer registration and follow the Cleaner thread behavior. Done means non-base-class FFIType entries no longer keep cleaner-managed memory alive indefinitely and the Cleaner thread can terminate its cleanup loop.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100