cachecontrol icon indicating copy to clipboard operation
cachecontrol copied to clipboard

Close CallbackFileWrapper.__buf once it's used to free memory.

Open elnuno opened this issue 8 years ago • 2 comments

This tries to solve #145. It seems like CallbackFileWrapper.__buf is keeping its stream after we're done with it, and it unnecessarily bloats the process.

Running the script at the end (and attached) against a 74 MB file I get...

For original code:

Using 106 MB on program end. Mean memory use: 199 MB

For both patched code and bare requests:

Using 32 MB on program end. Mean memory use: 32 MB

(These numbers are on Windows, Python 3.4.2 32-bits)

import os
import logging
import shutil

import requests
import cachecontrol
import psutil

# logging.basicConfig(level=logging.DEBUG)

us = psutil.Process(os.getpid())
MB = 1024 * 1024
N = 15

sess = cachecontrol.CacheControl(requests.Session())

total = 0
for i in range(N):
    url = 'http://localhost:8000/bigdata.bin?limit=%s' % i
    print('Requesting %s...' % i)
    response = sess.get(url, stream=True)
    fh = open('dest.bin', 'wb')
    shutil.copyfileobj(response.raw, fh)
    fh.close()
    used_mem = us.memory_full_info().uss / MB
    total += used_mem
    print('Using %d MB.' % round(used_mem))

import gc
gc.collect()

print('Done.')
print('Using %d MB on program end.' % round(us.memory_full_info().uss / MB))
print('Mean memory use: %d MB' % round(total/N))
input()

elnuno avatar Apr 12 '17 01:04 elnuno

I realize this is pretty old, but do you mind rebasing on the latest master?

ionrock avatar Jun 07 '18 14:06 ionrock

This was merged in #254 and can be closed.

itamarst avatar Oct 19 '21 13:10 itamarst