[Tutor] large strings and garbage collection

Dinesh B Vadhia dineshbvadhia at hotmail.com
Fri Jul 17 23:49:23 CEST 2009

This was discussed in a previous post but I didn't see a solution.  Say, you have 

for i in veryLongListOfStringValues:
    s += i

As per previous post (http://thread.gmane.org/gmane.comp.python.tutor/54029/focus=54139), (quoting verbatim) "... the following happens inside the python interpreter:

1. get a reference to the current value of s.
2. get a reference to the string value i.
3. compute the new value += i, store it in memory, and make a reference to it.
4. drop the old reference of s (thus free-ing "abc")
5. give s a reference to the newly computed value.

After step 3 and before step 4, the old value of s is still referenced by s, and the new value is referenced internally (so step 5 can be performed). In other words, both the old and the new value are in memory at the same time after step 3 and before step 4, and both are referenced (that is, they cannot be garbage collected). ... "

As s gets very large, how do you deal with this situation to avoid a memory error or what I think will be a general slowing down of the system if the for-loop is repeated a large number of times.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090717/0ef187c6/attachment.htm>

More information about the Tutor mailing list