Hi all!
I'm fighting a memory leak in a Python project and spent much time on it already. I have deduced the problem to a small example. Now seems like I know the solution, but I can't understand why.
import random
def main():
d = {}
used_keys = []
n = 0
while True:
# choose a key unique enough among used previously
key = random.randint(0, 2 ** 60)
d[key] = 1234 # the value doesn't matter
used_keys.append(key)
n += 1
if n % 1000 == 0:
# clean up every 1000 iterations
print 'thousand'
for key in used_keys:
del d[key]
used_keys[:] = []
#used_keys = []
if __name__ == '__main__':
main()
The idea is that I store some values in the dict d
and memorize used keys in a list to be able to clean the dict from time to time.
This variation of the program confidently eats memory never returning it back. If I use alternative method to „clear” used_keys
that is commented in the example, all is fine: memory consumption stays at constant level.
Why?
Tested on CPython and many linuxes.