<div>??<br clear="all"></div><div><br></div><div>Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on<br>win32<br>Type "help", "copyright", "credits" or "license" for more information.<br>

>>> import sys<br>>>> L = []<br>>>> for i in xrange(100000):<br>...     L.append(str(i) * (1000 / len(str(i))))<br>...<br>>>> sys.getsizeof(L)<br>824464<br>>>> L = []<br>>>> for i in xrange(20000):<br>

...     L.append(str(i) * (5000 / len(str(i))))<br>...<br>>>> sys.getsizeof(L)<br>178024<br>>>><br></div><div><br></div>~/santa<br>
<br><br><div class="gmail_quote">On Wed, Mar 16, 2011 at 11:20 AM, Amit Dev <span dir="ltr"><<a href="mailto:amitdev@gmail.com">amitdev@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

sum(map(len, l)) =>  99998200 for 1st case and 99999100 for 2nd case.<br>
Roughly 100MB as I mentioned.<br>
<div><div class="h5"><br>
On Wed, Mar 16, 2011 at 11:21 PM, John Gordon <<a href="mailto:gordon@panix.com">gordon@panix.com</a>> wrote:<br>
> In <<a href="mailto:mailman.988.1300289897.1189.python-list@python.org">mailman.988.1300289897.1189.python-list@python.org</a>> Amit Dev <<a href="mailto:amitdev@gmail.com">amitdev@gmail.com</a>> writes:<br>


><br>
>> I'm observing a strange memory usage pattern with strings. Consider<br>
>> the following session. Idea is to create a list which holds some<br>
>> strings so that cumulative characters in the list is 100MB.<br>
><br>
>> >>> l = []<br>
>> >>> for i in xrange(100000):<br>
>> ...  l.append(str(i) * (1000/len(str(i))))<br>
><br>
>> This uses around 100MB of memory as expected and 'del l' will clear that.<br>
><br>
>> >>> for i in xrange(20000):<br>
>> ...  l.append(str(i) * (5000/len(str(i))))<br>
><br>
>> This is using 165MB of memory. I really don't understand where the<br>
>> additional memory usage is coming from.<br>
><br>
>> If I reduce the string size, it remains high till it reaches around<br>
>> 1000. In that case it is back to 100MB usage.<br>
><br>
> I don't know anything about the internals of python storage -- overhead,<br>
> possible merging of like strings, etc.  but some simple character counting<br>
> shows that these two loops do not produce the same number of characters.<br>
><br>
> The first loop produces:<br>
><br>
> Ten single-digit values of i which are repeated 1000 times for a total of<br>
> 10000 characters;<br>
><br>
> Ninety two-digit values of i which are repeated 500 times for a total of<br>
> 45000 characters;<br>
><br>
> Nine hundred three-digit values of i which are repeated 333 times for a<br>
> total of 299700 characters;<br>
><br>
> Nine thousand four-digit values of i which are repeated 250 times for a<br>
> total of 2250000 characters;<br>
><br>
> Ninety thousand five-digit values of i which are repeated 200 times for<br>
> a total of 18000000 characters.<br>
><br>
> All that adds up to a grand total of 20604700 characters.<br>
><br>
> Or, to condense the above long-winded text in table form:<br>
><br>
> range         num digits 1000/len(str(i))  total chars<br>
> 0-9            10 1      1000                    10000<br>
> 10-99          90 2       500                    45000<br>
> 100-999       900 3       333                   299700<br>
> 1000-9999    9000 4       250                  2250000<br>
> 10000-99999 90000 5       200                 18000000<br>
>                                              ========<br>
>                          grand total chars   20604700<br>
><br>
> The second loop yields this table:<br>
><br>
> range         num digits 5000/len(str(i))  total bytes<br>
> 0-9            10 1      5000                    50000<br>
> 10-99          90 2      2500                   225000<br>
> 100-999       900 3      1666                  1499400<br>
> 1000-9999    9000 4      1250                 11250000<br>
> 10000-19999 10000 5      1000                 10000000<br>
>                                              ========<br>
>                          grand total chars   23024400<br>
><br>
> The two loops do not produce the same numbers of characters, so I'm not<br>
> surprised they do not consume the same amount of storage.<br>
><br>
> P.S.: Please forgive me if I've made some basic math error somewhere.<br>
><br>
> --<br>
> John Gordon                   A is for Amy, who fell down the stairs<br>
> <a href="mailto:gordon@panix.com">gordon@panix.com</a>              B is for Basil, assaulted by bears<br>
>                                -- Edward Gorey, "The Gashlycrumb Tinies"<br>
><br>
> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>