Fix SubInterpreter pyjmethod leak on static types. #579
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.
Each interpreter has a Python type corresponding to each Java class that has been used in that interpreter. Shared interpreters share a common collection of types but each sub-interpreter uses separate types to facilitate interpreters with different class loaders. Most types are dynamically allocated but the types for Object and Buffer are statically allocated because Buffer uses the python buffer protocol which is not available for dynamically allocated types in older versions of Python.
During interpreter initialization PyJMethods are added to the Python types so that Java methods can be called from Python. The static types get the correct methods added when the first interpreter is initialized but any interpreter created after that adds redundant methods to the static types. In long running programs that create and close many sub-interpreters the extra redundant methods can cause memory problems because each PyJMethod is holding onto a JNI Global reference to a Java Method object.. The redundant methods can also slow down method calls if they are called from Python.
This change adds a static variable in the native code to ensure the methods are only added to the static types once.