NumPy slice views keep deleted array's buffer alive — diagnosing retained memory
0 reputation · 18 Aug 2021, 22:29 UTC
0 reputation · 18 Aug 2021, 22:29 UTC
After deleting a large NumPy array, process memory does not drop as expected. A slice or reshaped view created earlier still references the original data buffer, so the underlying allocation stays alive even though the base array name is gone.
NumPy views share memory with their base array by design, and np.shares_memory can confirm the relationship, but there is no documented API to detach a view from its source buffer. The writeable flag and reference counting interact in ways that may differ across NumPy versions, and the retention may only become visible under heavy workloads. Assume a recent NumPy 1.26/2.x release; behavior should be verified against the exact version in use.
view.copy() plus deleting all references the only option?writeable flag itself affect garbage collection of the base buffer, or is retention purely a reference-counting effect?tracemalloc, psutil, or np.shares_memory checks after forced gc.collect()?There is no supported way to detach a NumPy view from its underlying buffer without copying the data. The buffer stays alive as long as any view (including slices, transposes, or reshaped arrays) holds a reference to it. The writeable flag does not influence this reference‑counting behavior.
data pointer as their base array; deleting the base variable only drops one reference.ndarray object reaches zero, which requires that all views no longer exist.view.writeable being False does not drop the reference; the buffer remains allocated regardless of the flag.view_copy = view.copy().del view (and any other aliases).del base.gc.collect() to reclaim the memory.None or let them go out of scope.gc.collect().view.base is None (if you still have a reference to the view) or use np.shares_memory(view, base) when the base is still accessible; both should indicate no sharing.psutil.Process().memory_info().rss or tracemalloc. A genuine drop in RSS after gc.collect() confirms the buffer was freed.The exact NumPy version can affect older behavior of the base attribute. If you are using a release prior to 1.13, please confirm your numpy.__version__ so we can version‑specific advice.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.