2

I'm transiting from Matlab/Octave to NumPy/SciPy. When I use Matlab in the interactive mode, I used clear or clear [some_variable] from time to time to remove that variable from memory. For example, before reading some new data to start a new sets of experiments, I used to clear data in Matlab.

How could I do the same thing with NumPy/SciPy?

I did some research, and I found there is a command called del, but I heard that del actually doesn't clear memory, but the variable disappears from the namespace instead. Am I right?

That being said, what would be the best way to mimic "clear" of Matlab in NumPy/SciPy?

braX
  • 11,506
  • 5
  • 20
  • 33
chanwcom
  • 4,420
  • 8
  • 37
  • 49
  • Use del. If an object isn't referenced, it will be deleted – Jens Munk Aug 25 '15 at 18:34
  • I would say "start a new notebook." If that doesn't mean anything to you, you really need to check out ipython notebooks, (http://ipython.org/notebook.html, http://opentechschool.github.io/python-data-intro/core/notebook.html) they will change your life I promise. – Bi Rico Aug 25 '15 at 19:15
  • @chanwcom, was your question answered or do you still need help? – Parker Nov 09 '15 at 07:27

1 Answers1

6

del(obj) will work, according to the scipy mail list

If you're working in IPython, then you can also use %xdel obj

...but I heard that "del" actually doesn't clear memory, but the variable disappears from the namespace. Am I right?

Yes, that's correct. That's what garbage collection is, Python will handle clearing the memory when it makes sense to, you don't need to worry about it, as from your end the variable no longer exists. Your code will behave the same, whether or not garbage collection has occurred yet won't matter, so there's no need for an alternative to del.

If you are curious about the differences of Matlab and Pythons garbage collection / memory allocation, you can read this SO thread on it.

Community
  • 1
  • 1
Parker
  • 8,539
  • 10
  • 69
  • 98