0

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?

asked Mar 1, 2019 at 4:45
2

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.