why python cache the string > 256?

s7v7nislands s7v7nislands at gmail.com
Tue Oct 27 03:42:54 CET 2009


On Oct 27, 4:03 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> s7v7nislands schrieb:
>
>
>
> > hi all:
>
> > test.py
> > #!/usr/bin/python
> > a = []
> > for i in xrange(1000000):
> >     a.append('a'*500)
>
> > $python -i test.py     #virt mem 514m in top output
> >>> del a                   #virt mem 510m
>
> > why python cache these string?
> > In source, I see when object size > SMALL_REQUEST_THRESHOLD, python
> > would use malloc.
> > also use free() when string refcount == 0.
>
> > do I miss somethong?
>
> http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-d...
>
> Diez

thanks. but it can't explain why cache string size bigger than
SMALL_REQUEST_THRESHOLD.
In source, It seems only allocate from cache pool when object's size <
SMALL_REQUEST_THRESHOLD (256),
when size > SMALL_REQUEST_THRESHOLD, it will direct use malloc.
see void *PyObject_Malloc(size_t nbytes) in Objects/obmalloc.c

I know the range & xrange's memory use. the int,str object has their
memory manager above Python's object allocator.
but why python don't free big string's memory, which size >
SMALL_REQUEST_THRESHOLD

sorry for poor english.
thanks!



More information about the Python-list mailing list