Skip to content

Fix SubInterpreter pyjmethod leak on static types.#579

Merged
bsteffensmeier merged 1 commit into
dev_4.2from
pyjmethod_leak
Dec 14, 2024
Merged

Fix SubInterpreter pyjmethod leak on static types.#579
bsteffensmeier merged 1 commit into
dev_4.2from
pyjmethod_leak

Conversation

@bsteffensmeier

Copy link
Copy Markdown
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
@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