Memory leak(?) Python 3.2 -
hi new python , have read enough posts specific subject none has specific answer. (using py 64-bit 3.2 edition)
i have big input read inside loop , read file create groups append list . process list , store inside file. unreffer list (list = none) , delete it. call gc collector manually. problem memory still been used. swap space , ram go wild.
for line in file: # read line line temp_buffer = line.split() # split elements word in temp_buffer: #enumerate (?) if not l1: # list empty l1.append(str(word)) #store '-' list else: # list not empty tempp = l1.pop(0) l1.insert(0,"-0") l1.sort(key=int) l2 = term_compress(l1) l1 = none # delete referrences del l1 # delete struct print(" ".join(str(i) in l2) , file=testfile) # print every term in file l2 = none # delete referrences del l2 # delete struct gc.collect() # run garbagge collector (free ram) l1 = [] l2 = [] l1.append(str(word)) what doing wrong ?
edit
example input:
-a 1 2 3 4 5 6 7 8 9 10 -n 7 8 9 10 11 12 13 14 15 ... output
-a 1# 10# -n 7# 15#
it's not memory/reference leak in traditional sense of programming error. you're seeing underlying c runtime aggressively holding on heap memory python allocated on behalf during loop. it's anticipating might need use memory again. holding onto cheaper giving os kernel ask again , again.
so, in short, though objects have been garbage collected in python runtime, underlying c runtime hangs onto heap memory in case program needs again.
from glibc documentation:
occasionally, free can return memory operating system , make process smaller. usually, can allow later call malloc reuse space. in meantime, space remains in program part of free-list used internally malloc.
in sense, memory utilization reported os after loop "peak memory utilization". if think it's high, have consider redesigning program limit peak memory usage. typically accomplished using kind of streaming or buffering design you're operating on smaller chunks of data @ time.
disclaimer, above layman's version , implementation specific various flavors of python, c, , os.
Comments
Post a Comment