Memory Leakage with Tuples and Lists

Rainer Deyke root at rainerdeyke.com
Sat Jan 6 12:25:06 EST 2001


"June Kim" <junaftnoon at nospamplzyahoo.com> wrote in message
news:936h6e$d11$1 at news.nuri.net...
> Tested under Python 1.5 and 2.0 on Win32:
>
> 1.
> Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)
> Type "copyright", "credits" or "license" for more informat
> >>> for j in range(10):
> ...     temp=range(800000)
> ...     del temp

This creates 799900 integer objects (each one separately allocated on the
heap and containing at least 12 bytes: 4 for data, 4 for type, and 4 for
reference count) and one list of 800000 elements at 4 bytes per element.
Minimum memory used: 12798800 bytes (12.2 MB).

> 2.
> Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)
> Type "copyright", "credits" or "license" for more informat
> >>> for j in range(10):
> ...     temp='a'*8000000
> ...     del temp

This creates a single string of 800000 characters at one byte per character.
Memory used: 800000 bytes.

> In the case of #2, there is no memory leakage at all. All the memory is
> returned 100%. However, in #1, there is a severe memory leakage, a few
mega
> bytes. Do you guys experience this on *nices as well?

All memory is allocated through malloc.  A typical malloc implementation
sends requests for large amounts of memory directly to the OS but deals with
requests for small amounts of memory by getting whole pages of memory from
the OS and handing out parts of these pages.  When the memory is released,
the big blocks return to the OS and the small pieces are kept by the
allocator to handle future requests.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list