Skip to content
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

Fix SubInterpreter pyjmethod leak on static types. #579

Merged
merged 1 commit into from
Dec 14, 2024

Conversation

bsteffensmeier
Copy link
Member

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.

@bsteffensmeier bsteffensmeier merged commit 1d14b4b into dev_4.2 Dec 14, 2024
2 checks passed
@bsteffensmeier bsteffensmeier deleted the pyjmethod_leak branch December 17, 2024 02:05
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