Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 10 commits into from
Jul 1, 2020

Conversation

mkitti
Copy link
Member

@mkitti mkitti commented Jun 18, 2020

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 and JNI.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 with JavaLocalRef and JavaGlobalRef 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 a JavaNullRef type and a constant J_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 via ccall I overloaded Ptr 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 call Ptr. To allow this, I plan on creating an abstract type in the JNI module. Concrete derivatives of that abstract type then should implement the Base.convert or Base.cconvert methods to convert the concrete type to Ptr{Nothing}. This would allow us to maintain the independence of the JNI module while allowing for alternate implementations.

@mkitti mkitti requested a review from aviks June 18, 2020 18:45
@aviks
Copy link
Collaborator

aviks commented Jun 19, 2020

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.

@mkitti
Copy link
Member Author

mkitti commented Jun 21, 2020

I just checked Taro.jl. The tests run well against this branch. The one issue is that I had to add write permissions to df-test.xlsx

@mkitti
Copy link
Member Author

mkitti commented Jun 21, 2020

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:
dfdx/Spark.jl#78


Pkg.test("Taro")
#include(joinpath(dirname(dirname(pathof(Taro))),"test","runtests.jl"))
try
Copy link
Collaborator

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?

Copy link
Member Author

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"
Copy link
Member Author

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".

@mkitti mkitti mentioned this pull request Jun 27, 2020
@mkitti mkitti merged commit caab713 into JuliaInterop:master Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants