frazman
frazman

Reputation: 33293

Resetting a dictionary to empty dictionary doesnt releases the memory?

So, I have a dictionary which I want to flush out once a day

So.. something like

d = {}

def refresh():
    global d
    now = datetime.datetime.now()
    if now >= TIME_TO_REFRESH:
       d  = {}

When I look into the memory profile, the memory grows as the size of d grows, but then when its time to refresh, I expect the memory to drop. The issue is that it does.. but not to a great extent. I am wondering if there is something else I need to do in order to make sure that the entire contents of the dictionary get flushed. Maybe its already happening but I am not able to find a good resource to see how the garbage collector works in this instance?

Upvotes: 0

Views: 752

Answers (2)

blami
blami

Reputation: 7431

In example above you are not really resetting that dictionary. You just assign new empty one to the existing name. To reset it you would need to call d.clear(). Even setting it to None will be just change in binding, not in the mutable object itself.

Python interpreter decides when to free memory using reference counting, which means that it counts how many references (names that point to actual memory structure) exists. If it drops to zero, memory is not used and can be freed. So if some other part of your program holds reference memory can't be freed. This is very simplified statement (see article I link below for further explanation).

Also component called garbage collector which executes the policy above might decide to delay freeing-up memory for various reasons. You can (but should not need to) force it by executing following code:

import gc
gc.collect()

You can find more on how garbage collection in Python works in this great article: https://rushter.com/blog/python-garbage-collector/

Upvotes: 2

Adam
Adam

Reputation: 4172

I would consider doing d.clear(). Reassigning creates a new dictionary and the old one will eventually be garbage collected but that is only once all other references to it are gone. .clear() will actually delete what is in the dictionary. https://stackoverflow.com/a/369925/3990806

Upvotes: 1

Related Questions