I got an issue about memory on my application when adding a lot object into dict then removing out of dict. But it seem python does not free the allocated memory.
Please see my example code:
from memory_profiler import profile
from twisted.internet import reactor
class MyClass(object):
def __init__(self):
n = 10
self.my_array = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
class TestClass(object):
token_names = dict()
counter = 0
def add(self):
self.counter = self.counter + 1
self.token_names[str(self.counter)] = MyClass()
def testrun(self):
while True:
if self.counter > 1000:
self.remove()
break
self.add()
@profile
def remove(self):
print("Remove " + str(len(self.token_names)))
for k, v in self.token_names.items():
del self.token_names[k]
self.token_names.clear()
@profile
def start(self):
self.testrun()
def main():
test = TestClass()
test.start()
reactor.run()
if __name__ == '__main__':
raise SystemExit(main())
When running this test, the memory growing up and does not free even the dict items were deleted from the dict after finish
But, if I modify the add() function to delete the newly added item from the dict likes that:
def add(self):
self.counter = self.counter + 1
self.token_names[str(self.counter)] = MyClass()
del self.token_names[str(self.counter)]
The memory does not increase in this test.
So, I don't know if I'm missing anything in this example or anything is wrong?
Can you all please help me on this?