JDK-8270851 : Logic for attaching/detaching native threads could be improved
  • Type: Bug
  • Component: tools
  • Sub-Component: jextract
  • Affected Version: 17,repo-panama
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-07-16
  • Updated: 2022-03-29
  • Resolved: 2022-03-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
repo-panamaFixed
Related Reports
Blocks :  
Description
Currently, we attach a native thread to VM before we jump into upcall code, and we later detach when we're done with the upcall.

In cases where the native library creates a thread pool, and keeps hitting upcalls with the same few threads, this scheme puts the VM under an enormous amount of pressure, as these threads are not recognized by the JVM, which will create a new Thread object each time (even though the underlying native thread is the same).

For instance, when experimenting with a Panama-based port of the Fuse library:

https://github.com/skymatic/fuse-panama

We noted that the VM was generating ~450G of memory in j.l.Thread instances which was obviously having a negative impact in terms of performance.

JNR does not suffer from this issue. JNR seem to feature an heuristics so that after a given thread has been attached enough times (1000) it is no longer detached. This works in the long run, although it does create a fair amount of garbage at the beginning of the application lifecycle.
Comments
This is still under discussion with Hotspot team. Some concerns were expressed at usage of thread local storage in the patch under review.
08-12-2021