-
-
Notifications
You must be signed in to change notification settings - Fork 51
Create JavaRef, JavaLocalRef, and JavaGlobalRef to hold references and manage memory #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Conceptually, this looks good. I'd like it if we could run tests for Spark.jl and Taro.jl on this branch, since those two package exercise the JVM much more than JavaCall tests can. |
I just checked Taro.jl. The tests run well against this branch. The one issue is that I had to add write permissions to |
I cannot get Spark.jl to work on Linux with Julia 1.3.1 to Julia 1.5-beta1 on Linux. I created an issue here: |
|
||
Pkg.test("Taro") | ||
#include(joinpath(dirname(dirname(pathof(Taro))),"test","runtests.jl")) | ||
try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we add it as a test depedency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added Taro as a test dependency by putting it in extras.
However, Taro is failing to work on Mac and this does not appear to be caused by this PR.
[extras] | ||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Taro = "61d0e4fa-4e73-5030-88a9-ae4c27b203dd" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aviks I added Taro as a test dependency on this like as an "extra".
Background: Local and Global Java References
Currently all Java references to objects in JavaCall are "local" Java references for variables that are only meant to live the lifetime of a Java method call after which the Java garbage collector may free the underlying memory. Because the use of these references in JavaCall is occurring outside of the context of a Java method call, currently the local references are never marked for garbage collection. The local part of a Java method call can emulated by the use of
JNI.PopLocalFrame
andJNI.PushLocalFrame
.However, there are some are some references that we would not like to mark for collection, notably the metaclasses. The JNI also has the concept of global references that are meant to survive a Java method call.
JavaRef, JavaLocalRef, JavaGlobalRef, and JavaNullRef types
To improve memory management, I create a new family of types,
JavaRef
withJavaLocalRef
andJavaGlobalRef
subtypes to track the scope of the underlying references. This also helps us to provide the correct deletion procedure for the references. Additionally, I create aJavaNullRef
type and a constantJ_NULL
to track null or deleted references.JProxy
This also permits advancement of #91 and #112 . JProxy makes some alterations to core JavaCall.jl to introduce global references. This is an alternative design that allows for both local and global references. We will thus backout the changes in to core JavaCall in #112 and use this Java reference management design instead.
Technical Notes
To facilitate easy extraction of the
Ptr{Nothing}
to pass to the JNI viaccall
I overloadedPtr
to replace the reference to the.ptr
field of JavaObject.Pending
For many JNI calls, it would be nice to simply pass the
JavaObject
rather than having to callPtr
. To allow this, I plan on creating an abstract type in the JNI module. Concrete derivatives of that abstract type then should implement theBase.convert
orBase.cconvert
methods to convert the concrete type toPtr{Nothing}
. This would allow us to maintain the independence of theJNI
module while allowing for alternate implementations.