[
https://issues.apache.org/jira/browse/COMPRESS-473?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16702468#comment-16702468
]
Roman Leventov commented on COMPRESS-473:
-----------------------------------------
In OpenJDK, Deflater and Inflater operate via a bunch of *static* native
methods. It allows to implement a happy path workaround:
1) Try to access all those methods via reflection and call setAccessible(true)
on them.
2) If any of the methods is not available or there are exceptions like
IllegalAccessException, retreat to the default path (just using
Deflater/Inflater)
3) If the methods are accessible, transform them into MethodHandles using
[MH.unreflect()|https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandles.Lookup.html#unreflect-java.lang.reflect.Method-]
4) Re-implement Inflater and Deflater using those MethodHandles and Cleaner.
The overhead of MethodHandle is minor compared to the overhead of native.
This method avoids dealing with JNI altogether. The disadvantage is that to
work on Java 9+, it will require cooperation from users, adding {{--add-opens
java.base/java.util.zip=ALL-UNNAMED}} runtime option, as described
[here|https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-12F945EB-71D6-46AF-8C3D-D354FD0B1781].
> Avoiding Java native peer objects that override finalize()
> ----------------------------------------------------------
>
> Key: COMPRESS-473
> URL: https://issues.apache.org/jira/browse/COMPRESS-473
> Project: Commons Compress
> Issue Type: Improvement
> Reporter: Roman Leventov
> Priority: Major
>
> Objects that override finalize() are very bad for GC. The infrastructure
> library such as Commons Compress is probably the right place to do the hard
> work of eliminating dependencies on such objects. Instead, PhantomReference
> registered in a Queue, or sun.misc.Cleaner (or java.lang.ref.Cleaner) should
> be used.
> In particular:
> java.util.zip.Deflater/Inflater/ZipOutputStream/ZipInputStream/GZipOutputStream/GZipInputStream
> should not be used.
>
> The issue for Zstd created in a downstream library:
> [https://github.com/luben/zstd-jni/issues/83]
>
> Some other downstream dependencies could be affected too.
>
> Commons Compress's own ZipFile and BZip2CompressorOutputStream classes should
> not override finalize() too.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)