Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dictionary's clear() method delete all the item related objects from memory?

If a dictionary contains mutable objects or objects of custom classes (say a queryset, or a even a DateTime), then will calling clear() on the dictionary delete these objects from memory? Does it behave differently than looping through the dict and deleting them?

eg. consider

class MyClass(object):
    '''Test Class.'''

my_obj_1 = MyClass()
my_obj_2 = MyClass()

my_dict = { 'foo' : my_obj_1, 'bar' : my_obj_2 }

then is

my_dict.clear()

same as

for key in my_dict.keys():
    del my_dict[key]

?

like image 736
0xc0de Avatar asked May 04 '12 10:05

0xc0de


People also ask

What is the purpose of the Clear () method in a dictionary?

The clear() method removes all the elements from a dictionary.

Can I use Clear () method to delete the dictionary?

Python dictionary clear() method is used to remove all elements from the dictionary. When you have a dictionary with too many elements, deleting all elements of the dictionary one after another is a very time taken process, so use clear() method to delete all elements at once rather than deleting elements one by one.

Which method is used to remove all items of a dictionary in python?

The clear() method helps clear all the items in the dictionary.

Which of the following lines of Python code will remove all the items from a dictionary named D?

The dict. clear() to delete a dictionary in Python The clear() method deletes all the key-value pairs present in the dict and returns an empty dict.


2 Answers

Python documentation on dicts states that del d[key] removes d[key] from the dictionary while d.clear() removes every key, so basically their behavior is the same.

On the memory issue, in Python when you "delete" you are basically removing a reference to an object. When an object is not referenced by any variable nor other object or becomes unreachable, it becomes garbage and can be removed from memory. Python has a garbage collector that from time to time it does this job of checking which objects are garbage and releases the memory allocated for them. If the object you are deleting from the dictionary is referenced by other variable then it is still reachable, thus it is not garbage so it won't be deleted. I leave you here some links if you are interested in reading about garbage collection in general and python's garbage collection in particular.

  • http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
  • http://www.digi.com/wiki/developer/index.php/Python_Garbage_Collection
  • http://www.doughellmann.com/PyMOTW/gc/
  • http://docs.python.org/library/gc.html
like image 153
Santiago Alessandri Avatar answered Oct 07 '22 15:10

Santiago Alessandri


There is in fact a very small difference between the two. clear() will free the memory of the hashset used in the dict, while removing the key will not.

a = dict.fromkeys(range(1000))

In [10]: sys.getsizeof(a)
Out[10]: 49432

In [11]: a.clear()

In [12]: sys.getsizeof(a)
Out[12]: 280

In [13]: a = dict.fromkeys(range(1000))

In [14]: for i in range(1000):
   ....:     del a[i]
   ....:     

In [15]: sys.getsizeof(a)
Out[15]: 49432
like image 25
317070 Avatar answered Oct 07 '22 16:10

317070