Frage

Is the memory shared between the StringIO if I do that? I have the feeling it is because the memory of the python process did not increase on line 6.

In [1]: from StringIO import StringIO
In [2]: s = StringIO()
In [3]: s.write('abcd'*10000000) # memory increases
In [4]: s.tell()
Out[4]: 40000000
In [5]: s.seek(0)
In [6]: a = StringIO(s.read()) # memory DOES NOT increase
In [7]: a.tell()
Out[7]: 0
In [8]: a.read(10)
Out[8]: 'abcdabcdab'

However my concern is that when I delete those 2 variables, the memory consumption of the python process does not decrease anymore... why ? Does this code create a memory leak ?

When I just used one variable, the memory is well freed when I delete the variable.

I'd be curious to better understand what is going on here. Thanks.

War es hilfreich?

Lösung

A StringIO() object does not make a copy of a string passed to it. There is no need to, as strings are not mutable.

When reading data from a StringIO() object in chunks, new string objects are created that are substrings from the original input string.

Memory consumption never goes down immediately when freeing objects. Memory allocation is only redistributed as needed by the OS, and many types of (small) objects can be interned by Python for efficiency and are never freed, only reused.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top