API/C memory mananegemnt problem

Tim Peters tim.peters at gmail.com
Fri Mar 10 23:52:26 EST 2006


[fumana at lambrate.inaf.it]
> Hi everybody,
> I have a problem with Python/C API and memory management.
>
> I'm using
> Python 2.3.5 (#1, Jan  4 2006, 16:44:27)
> [GCC 4.0.2 20050901 (prerelease) (SUSE Linux)] on linux2
>
> In my C-module I have a loop like this:
> ***********************************************
>
> int size=10000000;
>
> output=(double *) calloc(size, sizeof(double));
>
> py_output=PyList_New(0);
>
> for(i=0; i<size; i++){
>   tmp=PyFloat_FromDouble(output[i]);
>   PyList_Append(py_output, tmp);
> }
>
> free(outout);
>
> return py_output;
>
> ***********************************************
>
> It returns to python module a (very large) list.
>
> Problem: when I delete the list in python module (with python del statement)
> not all memory is relased.
>
> It look like all 10000000 tmp PyFloat allocated in C code
> remain stored in memory.
>
> Somebody can help me?

As Fredrik noted, your refcounting is off and you need a great deal of
error-checking code.

However, none of that will help your underlying problem:  CPython
floats are allocated in a special free list dedicated to floats, and
that free list is both immortal and unbounded in size.  Once Python
has allocated memory to hold a Python float, that memory is never
released.  BTW, Python has another, distinct immortal and unbounded
free list dedicated to holding storage for int objects.

There's nothing you can do about that.  It may be possible for you to
use an array.array to hold "unboxed" floats directly; that depends on
details of your app.



More information about the Python-list mailing list