Pass objects into JNI code in addition to raw pointer value (prevent premature GC)#700
Merged
matthiasblaesing merged 1 commit intojava-native-access:masterfrom Sep 24, 2016
Conversation
…premature GC)
The Memory/Pointer object delegates the native calls to static methods
in the Native class. This causes the Memory/Pointer object to become
prematurely eligable for GC for this case:
----------------------------
Memory pointer = <init>;
<do something and work on Memory>
String result = pointer.getWideString(0)
<do nothing more with Memory>
----------------------------
getWideString(0) calls back into the JVM and allocates a new java string.
In this case the GC can become active and collect the Memory object.
After this it is possible to run the finalizer, that in turn frees the
native memory backing the memory object. The native function now
possibly accesses freed memory.
The native code needs to hold references to the relevant object itself,
this is implemented here.
Changes:
API relevant changes:
- Deprecate Native#getDirectByteBuffer(long, long)
it is functional, but discouraged to be used and should be removed in
future
JNI-Layer (Native.java + dispatch.c):
- All set<Type> and get<Type>, write and read methods that took a
pointer address are modified to take a pointer object, the raw pointer
value and an offset
- All invoke<ReturnType> methods that took a pointer adress (function
pointer) are modified to take a function object and the raw function
pointer value
Build-System/Native:
- the jni api version as bumped from 5.0.0 to 5.1.0 (the major version
was raised in the 4.3.0 development cycle, so not modified)
- all prebuild native libraries were replaced with the "OUT_OF_DATE"
marker, where no replacement could be build immediately
getDirectByteBuffer(long, long) is the only API relevant method, allother
methods modified are package private and so are not considered API.
getDirectByteBuffer is overloaded with a package private variant
getDirectByteBuffer(Pointer, long, long, long) that reflects the above
described changes.
Executed tests:
- linux-amd64: all unittests of jna pass
- linux-arm: 1 unittest fails (unload of jnalib, emulated
environment (qemu), slow execution leads to timeout)
- linux-x86: all unittests of jna pass (32bit chroot on 64bit kernel)
- windows-64bit: 2 tests fail (before and after: DirectTypeMapperTest
and NativeTest#testSizeOf (bool should be 1, is 4)
- windows-32bit: 2 tests fails (one less that before the change
(testLongStringGeneration), other errors see x64)
Performance:
Check was done with JMH - the comparison was between master and the
proposed pointer_native_direct branch:
java -jar benchmarks-master.jar -i 10 -wi 10 -f 5
The benchmarks initialize a Memory block and access it via getInt and
getString, one with an offset of 0 and on with an offset of 5 bytes.
The testmatrix:
testInt testIntWithOffset testString testStringWithOffset
linux64-jdk7 108% 108% 100% 101%
linux64-jdk8 107% 108% 100% 100%
linux32-jdk8 97% 97% 112% 112%
win32-jdk8 90% 95% 98% 98%
win64-jdk8 95% 95% 97% 97%
This was referenced Sep 17, 2016
mstyura
pushed a commit
to mstyura/jna
that referenced
this pull request
Sep 9, 2024
Motivation: Netty 4.1.108.Final was released Modifications: Update to latest release Result: Depend on latest release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the follow up to the discussion, that happened in #664. This implements the option,
that passes the Pointer/Function object to native in addition to the raw pointer value.
This breaks the JNI layer, but not an outside facing API. Please see the commit message for details.
In addition to the summary in the commit message the raw numbers and benchmarks can be found here:
Raw numbers: http://www.doppel-helix.eu/Pointer_to_Native_Comparison2.ods
Complete material: http://www.doppel-helix.eu/Pointer_to_Native_Comparison2.7z
I added the native parts I could build ad-hoc:
I have a plan and done all android builds, but I suggest to evaluate first if #669 should be merged, if so the merge should be done before a full rebuild.
Closes: #664