memory leak with large list??

Skip Montanaro skip at pobox.com
Fri Jan 24 18:33:28 EST 2003


    >> %MEM  RSS  RSS    SZ   VSZ
    >> 46.2 237648 237648 59690 238760

    >> So then delete it.

    >>>> del(big_list)

    >> But the memory footprint of the process remains very large:

    >> %MEM  RSS  RSS    SZ   VSZ
    >> 37.1 190772 190772 47971 191884

    >> Is python leaking memory, or is there something else going on here.

No, this is your platform's malloc() library not returning memory to the
operating system.

    >> I should note, if I just preallocate the list with 0.0:

    >>>> big_list = [0.0] * 12000000

    >> and then delete it, I don't see the leak, but then memory footprint is
    >> smaller than you would expect
    >> from 12 million floats:

    >> %MEM  RSS  RSS    SZ   VSZ
    >>  9.4 48700 48700 12453 49812

    >> So I think python is using a shortcut in this case.

Yes.  a float for 0.0 is allocated once, then referred to from all those
slots.  Consider this example:

    >>> list1 = [0.0] * 5
    >>> list2 = [float(0) for f in xrange(5)]
    >>> list1
    [0.0, 0.0, 0.0, 0.0, 0.0]
    >>> list2
    [0.0, 0.0, 0.0, 0.0, 0.0]
    >>> list1 == list2
    True
    >>> map(id, list1)
    [6355924, 6355924, 6355924, 6355924, 6355924]
    >>> map(id, list2)
    [6355860, 6355908, 6355892, 6355876, 6355844]

Skip





More information about the Python-list mailing list