Skip to content

Run Python garbage collector when an interpreter is closed.#556

Merged
bsteffensmeier merged 1 commit into
ninia:dev_4.2from
bsteffensmeier:gc-on-close
Sep 16, 2024
Merged

Run Python garbage collector when an interpreter is closed.#556
bsteffensmeier merged 1 commit into
ninia:dev_4.2from
bsteffensmeier:gc-on-close

Conversation

@bsteffensmeier

Copy link
Copy Markdown
Member

This change will run Python Garbage Collection anytime an interpreter is closed.

#555 describes a simple use case where reference counting alone is unable to free memory in a jep interpreter on close and running the garbage collector cleans up the memory.

There is a potential downside that this will make close slower in some cases however it should be negligible in most cases and I believe the benefit of freeing memory promptly is worth the extra time in close().

@ndjensen ndjensen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Comment thread src/main/c/Jep/pyembed.c
Py_DECREF(key);

Py_CLEAR(jepThread->globals);
PyGC_Collect();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why do you the gc collect call before the Py_EndInterpreter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not specifically documented for PyGC_Collect but my default assumption for any Python c-api call is that it requires the thread to have an interpreter or at least hold the GIL and after Py_EndInterpreter the thread has neither.

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