From 8af9d2a19aff0a50988c7412142bbf5e161b7076 Mon Sep 17 00:00:00 2001 From: Haixuan Xavier Tao Date: Sat, 7 Jan 2023 15:52:55 -0500 Subject: [PATCH] Add a section on memory management for `extension` Adding a special case of memory management when writing an extension. This is a documentation of: https://github.com/PyO3/pyo3/issues/1056 and https://github.com/PyO3/pyo3/issues/2853 --- guide/src/memory.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/guide/src/memory.md b/guide/src/memory.md index 59484589..715c78f9 100644 --- a/guide/src/memory.md +++ b/guide/src/memory.md @@ -68,6 +68,10 @@ bound to the `GILPool`, not the for loop. The `GILPool` isn't dropped until the end of the `with_gil()` closure, at which point the 10 copies of `hello` are finally released to the Python garbage collector. +This is often the case when writing extension as `#[pyfunction]` or `#[pymethod]` will lock +the `GILPool` at the beginning of the function call and only release it at +the returns. For more on this issue: [#1056](https://github.com/PyO3/pyo3/issues/1056) + In general we don't want unbounded memory growth during loops! One workaround is to acquire and release the GIL with each iteration of the loop.