Memory Usage of Strings

Amit Dev amitdev at gmail.com
Wed Mar 16 11:38:12 EDT 2011


I'm observing a strange memory usage pattern with strings. Consider
the following session. Idea is to create a list which holds some
strings so that cumulative characters in the list is 100MB.

>>> l = []
>>> for i in xrange(100000):
...  l.append(str(i) * (1000/len(str(i))))

This uses around 100MB of memory as expected and 'del l' will clear that.


>>> for i in xrange(20000):
...  l.append(str(i) * (5000/len(str(i))))

This is using 165MB of memory. I really don't understand where the
additional memory usage is coming from.

If I reduce the string size, it remains high till it reaches around
1000. In that case it is back to 100MB usage.

Python 2.6.4 on FreeBSD.

Regards,
Amit



More information about the Python-list mailing list