I have a Python2.7 App which used lots of dict
objects which mostly contain strings for keys and values.
Sometimes those dicts and strings are not needed anymore and I would like to remove those from memory.
I tried different things, del dict[key]
, del dict
, etc. But the App still uses the same amount of memory.
Below a example which I would expect to fee the memory. But it doesn't :(
import gc
import resource
def mem():
print('Memory usage : % 2.2f MB' % round(
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024.0/1024.0,1)
)
mem()
print('...creating list of dicts...')
n = 10000
l = []
for i in xrange(n):
a = 1000*'a'
b = 1000*'b'
l.append({ 'a' : a, 'b' : b })
mem()
print('...deleting list items...')
for i in xrange(n):
l.pop(0)
mem()
print('GC collected objects : %d' % gc.collect())
mem()
Output:
Memory usage : 4.30 MB
...creating list of dicts...
Memory usage : 36.70 MB
...deleting list items...
Memory usage : 36.70 MB
GC collected objects : 0
Memory usage : 36.70 MB
I would expect here some objects to be 'collected' and some memory to be freed.
Am I doing something wrong? Any other ways to delete unused objects or a least to find where the objects are unexpectedly used.