I have a question about Numpy array memory management. Suppose I create a numpy array from a buffer using the following:
>>> s = "abcd"
>>> arr = numpy.frombuffer(buffer(s), dtype = numpy.uint8)
>>> arr.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : False
ALIGNED : True
UPDATEIFCOPY : False
>>> del s # What happens to arr?
In the situation above, does 'arr' hold a reference to 's'? If I delete 's', will this free the memory allocated for 's' and thus make 'arr' potentially referencing unallocated memory?
Some other questions I have:
- If this is valid, how does Python know when to free the memory allocated by 's'? The gc.get_referrents(arr) function doesn't seem to show that 'arr' holds a reference to 's'.
- If this is invalid, how can I register a reference to 's' into 'arr' so that Python GC will automatically reap 's' when all references to it are gone?