[Numpy-discussion] Deleting arrays.

David M. Cooke cookedm at physics.mcmaster.ca
Wed Feb 25 02:58:02 CST 2004


On Tue, Feb 24, 2004 at 10:10:51PM +0100, Andrea Riciputi wrote:
> Hi,
> I'm running a simulation program partly written in C partly in Python 
> and I use Numeric arrays extensively. At the end of a single run I have 
> a bunch of Numeric arrays containing partial results that I don't need 
> anymore. These arrays have been created on the C side of my code and 
> then copied (actually linked via PyArray_FromDimsAndData) to Numeric 
> arrays. How can I release these arrays and get back memory in order to 
> be able to start the next run?

Simple answer: don't use PyArray_FromDimsAndData. Allocate your array
with Python using PyArray_FromDims, and use the data element for your C
array, instead of allocating using C and convincing Python to use that.

However, there are instances where you can't do that (the memory was
allocated by some library, for instance, that needs a custom
deallocator, or is from another array type). Then I'd suggest 

1) copying the data (depends on how much there is)
2) if the array memory was originally allocated using malloc, you set
the OWNDATA flag so Python will free it (going from memory here on the
name; check the source). Note that you should be really sure you and
Python are using the same allocator/deallocator routines.
3) or using the following trick, best described with some code:

MyOtherArrayType *A = a_library_routine_that_allocates_a_routine();
PyObject *oA = PyArray_FromDimsAndData(rank, dims, PyArray_DOUBLE,
                                      (char *)A->array_data);
PyObject *w = PyCObject_FromVoidPtr(A, free_myotherarraytype);
((PyArrayObject *)oA)->base = w;

Now, when oA is DECREF'd latter, oA->base will also be DECREF'd. Since
it's a PyCObject, free_myotherarraytype(A) will be called, free'ing the
data. Since PyArray_FromDimsAndData was used, Python won't try to
deallocate oA->data.

I haven't checked whether this works under numarray (and I'm sure I saw
a better way to do it there). I have used it for a Numeric<->Blitz++
wrapper.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca




More information about the Numpy-discussion mailing list