Memory leak when adding list values

I am new to python and have a big memory problem. my script runs 24/7 and every day it allocates about 1 GB of my memory. I could narrow it down to this function:

Code:

#!/usr/bin/env python
# coding: utf8
import gc
from pympler import muppy
from pympler import summary
from pympler import tracker


v_list = [{ 
     'url_base' : 'http://www.immoscout24.de',
     'url_before_page' : '/Suche/S-T/P-',
     'url_after_page' : '/Wohnung-Kauf/Hamburg/Hamburg/-/-/50,00-/EURO--500000,00?pagerReporting=true',}]

# returns url
def get_url(v, page_num):
    return v['url_base'] + v['url_before_page'] + str(page_num) + v['url_after_page']


while True:
    gc.enable()

    for v_idx,v in enumerate(v_list):

        # mem test ouput
        all_objects = muppy.get_objects()
        sum1 = summary.summarize(all_objects)
        summary.print_(sum1)


        # magic happens here
        url = get_url(v, 1)


        # mem test ouput
        all_objects = muppy.get_objects()
        sum1 = summary.summarize(all_objects)
        summary.print_(sum1)

        # collects unlinked objects
        gc.collect()

Output:

======================== | =========== | ============
                    list |       26154 |     10.90 MB
                     str |       31202 |      1.90 MB
                    dict |         507 |    785.88 KB

expelially the list attribute is getting bigger and bigger, each cycle is about 600kb, and I have no idea why. in my opinion, I do not store anything here, and the url variable should be rewritten every time. therefore basically should be memory consumption in general.

What am I missing here? :-)

+2
source share
1 answer

" " 100% . all_objects , , - , , , , all_objects, .

:

  • , list, 600 /, , , 20 , .

  • del all_objects sum1 =, list, 100 650 .

, , . , muppy.get_objects() ( ), all_objects . , , . , all_objects, , refcount 2 1. , - , .

, , . muppy.get_objects del all_objects. ( , , sum1 = .)

+3

All Articles