0

I have this python class in which I need to do

self.data = copy.deepcopy(raw_data)

raw_data is a dictionary of a dictionary and takes many megabytes in memory. I only need the data once (in which I do some modification to the data thus the need to do a deepcopy) and I would like to destroy the deepcopy data once I'm done with the computation.

What would be the best way to clear the data from the memory?

Would this work?

self.data = None

Note I'm using Python 3.4 if it makes a difference.

K-Jtan
  • 64
  • 1
  • 9
  • Note I'm new to python and have a C++ background (so I'm looking for something like a c++ **delete**) – K-Jtan Aug 31 '15 at 12:52
  • 1
    [del](http://stackoverflow.com/questions/6146963/when-is-del-useful-in-python) is probably the keyword you want? It removes the reference, which will allow the garbage collector to take care of it. – NightShadeQueen Aug 31 '15 at 12:57
  • @NightShadeQueen Yes that seems to be the answer. **del** is used to delete entire variables. https://docs.python.org/3.4/tutorial/datastructures.html?highlight=del#the-del-statement – K-Jtan Aug 31 '15 at 13:08

2 Answers2

0

Some say it's not neccesary that python will do it for you, as long as you don't use the varaible for some time. Some say to use garabage collector library.

According Havenard and to Python Official Documentation, you can force the Garbage Collector to release unreferenced memory with gc.collect()

For more information on this:

How can I explicitly free memory in Python?

Community
  • 1
  • 1
taesu
  • 4,482
  • 4
  • 23
  • 41
  • Thank you for the quick reply. Let me read the documentation and come back with some feed back – K-Jtan Aug 31 '15 at 12:58
  • 1
    it does seems that you can specify which variable to be pass to the garbage collector. Of what I could see, you can call `del self.data` and it will then call the garbage collector. – K-Jtan Aug 31 '15 at 13:14
0

You will have to stop referencing the object both directly and indirectly. It maybe that setting self.data to None is not enough. del self.data does about the same thing, but instead of setting self.data to None it removes the attribute data from self instead.

CPython (the normal implementation of python) uses primary reference counting to determine when an object may be collected, when reference count has dropped to zero it will be collected immediately. You can check the reference count of self.data by using sys.getrefcount(self.data), but that might confuse you as it may report one more reference since the function itself has a reference to the object.

skyking
  • 13,817
  • 1
  • 35
  • 57