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
This commit is contained in:
Haixuan Xavier Tao 2023-01-07 15:52:55 -05:00 committed by David Hewitt
parent 94c568d746
commit 8af9d2a19a
1 changed files with 4 additions and 0 deletions

View File

@ -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.