Memory Usage of Strings

eryksun () eryksun at gmail.com
Wed Mar 16 16:13:05 EDT 2011


On Wednesday, March 16, 2011 2:20:34 PM UTC-4, Amit Dev wrote:
>
> sum(map(len, l)) =>  99998200 for 1st case and 99999100 for 2nd case.
> Roughly 100MB as I mentioned.

The two lists used approximately the same memory in my test with Python 2.6.6 on Windows. An implementation detail such as this is likely to vary between interpreters across versions and platforms, including Jython and IronPython. A classic implementation detail is caching of small objects that occur frequently, such as small strings, integers less than 512, etc. For example:

In [1]: a = 20 * '5'
In [2]: b = 20 * '5'
In [3]: a is b
Out[3]: True

In [4]: a = 21 * '5'
In [5]: b = 21 * '5'
In [6]: a is b
Out[6]: False

It's best not to depend on this behavior.



More information about the Python-list mailing list