Python memory usage

Fredrik Lundh fredrik at pythonware.com
Mon Nov 13 15:51:11 EST 2006


Klaas wrote:

> I think floats use obmalloc so I'm slightly surprised you don't see
> differences.

as noted in the FAQ I just posted a link to, floats also use a free list 
(using pretty much identical code to that used for integers).

see comments in Objects/intobject.c (quoted below) and 
Objects/floatobject.c for details.

</F>

/* Integers are quite normal objects, to make object handling uniform.
    (Using odd pointers to represent integers would save much space
    but require extra checks for this special case throughout the code.)
    Since a typical Python program spends much of its time allocating
    and deallocating integers, these operations should be very fast.
    Therefore we use a dedicated allocation scheme with a much lower
    overhead (in space and time) than straight malloc(): a simple
    dedicated free list, filled when necessary with memory from malloc().

    block_list is a singly-linked list of all PyIntBlocks ever allocated,
    linked via their next members.  PyIntBlocks are never returned to the
    system before shutdown (PyInt_Fini).

    free_list is a singly-linked list of available PyIntObjects, linked
    via abuse of their ob_type members.
*/

#define BLOCK_SIZE	1000	/* 1K less typical malloc overhead */
#define BHEAD_SIZE	8	/* Enough for a 64-bit pointer */




More information about the Python-list mailing list