了解内存消耗:释放一个StringIO的内存?

时间:2013-03-06 21:40:15

标签: python memory buffer

我很想知道为什么在第一个例子中,内存消耗就像我在成像一样:

s = StringIO()
s.write('abc'*10000000)
# Memory increases: OK
s.seek(0)
s.truncate()
# Memory decreases: OK

而在第二个例子中,最后,我使用相同的东西,但截断方法后内存似乎没有减少。 以下代码位于类的方法中。

from StringIO import StringIO
import requests

self.BUFFER_SIZE = 5 * 1024 * 2 ** 10 # 5 MB
self.MAX_MEMORY = 3 * 1024 * 2 ** 10 # 3 MB

r = requests.get(self.target, stream=True)  # stream=True to not download the data at once

chunks = r.iter_content(chunk_size=self.BUFFER_SIZE)
buff = StringIO()

# Get the MAX_MEMORY first data
for chunk in chunks:
    buff.write(chunk)
    if buff.len > self.MAX_MEMORY:
        break

# Left the loop because there is no more chunks: it stays in memory
if buff.len < self.MAX_MEMORY:
    self.data = buff.getvalue()

# Otherwise, prepare a temp file and process the remaining chunks
else:
    self.path = self._create_tmp_file_path()

    with open(self.path, 'w') as f:
        # Write the first downloaded data
        buff.seek(0)
        f.write(buffer.read())

        # Free the buffer ?
        buff.seek(0)
        buff.truncate()

        ###################
        # Memory does not decrease
        # And another 5MB will be added to the memory hiting the next line which is normal because it is the size of a chunk
        # But if the buffer was freed, the memory would stay steady: - 5 MB + 5 MB

        # Write the remaining chunks directly into the file
        for chunk in chunks:
            f.write(chunk)

有什么想法? 感谢。

0 个答案:

没有答案