After testing, seems like the underlying reason is exactly what @juanpa suspected.
Basic indexing in numpy returns a view and not a copy of the array, so the underlying buffer is still held. You can access the original array using a[0].base
.
If however you'd create a copy of the array like so: a = [i.copy() for i in a]
, you'd suddenly see a drop in the allocated memory, as the original objects would all be lost and cleared.
Do note however, that if you allocate other objects between the slicing and the copying, you might not release that memory back due to fragmentation.
Run this code and you'll see the difference:
import os, psutil
import numpy as np
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
def append(x):
x.append(np.random.normal(size=(1000,1000)))
a = []
append(a)
append(a)
append(a)
print(process.memory_info().rss)
a = [i[:10].copy() for i in a] # Copy the array.
print(process.memory_info().rss)