Memory management

I have a question about memory management of a Numpy array. 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 above situation, does 'arr' contain a reference to 's'? If I delete 's', will it free the memory allocated for 's' and thus make 'arr' potentially a reference to unallocated memory?

Some other questions that I have:

  • If this is true, how does Python know when to free the memory allocated by 's'? The gc.get_referrents (arr) function does not seem to show that 'arr' contains a reference to 's'.
  • If this is unacceptable, how can I register a reference to 's' on 'arr' so that the Python GC automatically retrieves 's' when all references to it have disappeared?
+4
source share
1 answer

The following should clarify the situation a bit:

 >>> s = 'abcd' >>> arr = np.frombuffer(buffer(s), dtype='uint8') >>> arr.base <read-only buffer for 0x03D1BA60, size -1, offset 0 at 0x03D1BA00> >>> del s >>> arr.base <read-only buffer for 0x03D1BA60, size -1, offset 0 at 0x03D1BA00> 

In the first case, del s has no effect, because what the array indicates is a buffer created from it, which is not mentioned anywhere.

 >>> t = buffer('abcd') >>> arr = np.frombuffer(t, dtype='uint8') >>> arr.base <read-only buffer for 0x03D1BA60, size -1, offset 0 at 0x03C8D920> >>> arr.base is t True >>> del t >>> arr.base <read-only buffer for 0x03D1BA60, size -1, offset 0 at 0x03C8D920> 

In the second case, when you del t , you get rid of the variable t pointing to the buffer object, but since the array still has a reference to the same buffer , it is not deleted, although I'm not sure how to check this if you are now del arr , the buffer object should lose its last link and automatically collect garbage.

+5
source

All Articles