Skip to content

Commit

Permalink
Add a section on memory management for extension
Browse files Browse the repository at this point in the history
Adding a special case of memory management when writing an extension.

This is a documentation of: PyO3#1056 and PyO3#2853
  • Loading branch information
haixuanTao authored and davidhewitt committed Jan 10, 2023
1 parent 94c568d commit 8af9d2a
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions guide/src/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 8af9d2a

Please sign in to comment.