I am trying to figure out how to release the space occupied by a python object while working on jupyter notebooks.
In the specific scenario, i am working on multiple large dataframes and once I have finished doing some computations, i would like to load up a new dataframe.
However this always causes a memory error and my system starts freezing up.
So i figured I will try deleting the python object.
Things i have tried so far :
1. Using del and invoking garbage collector
del pyobject
import gc
gc.collect()
Using ipython magic commands
%reset_selective -f pyobject
Both these approaches fail to actually release the memory. Looking at the htop command shows me that still 20GB of my RAM is being used up by the jupyter notebook. They memory is only deallocated when i restart the kernel (but then i end up losing all the other variables i require for my next stage of data analysis)
If the notebook or your code is acting weird sometimes it is best to press the “RESET BUTTON”. Reseting the kernel clears all in memory objects and restarts your code from the very top.
To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global . To clear a particular class, use clear myClass . To clear a particular function or script, use clear functionName .
Python's memory allocation and deallocation method is automatic. The user does not have to preallocate or deallocate memory by hand as one has to when using dynamic memory allocation in languages such as C or C++. Python uses two strategies for memory allocation reference counting and garbage collection.
Press 'control-shift-p', that opens the command palette. Then type 'clear cell output'. That will let you select the command to clear the output.
Make sure that no other references exist to your data. Are there no other views based on that array? You can get the objects that hold a reference to something using get_referrers
If there are any views you should promote those to copies.
In particular ipython tends to keep references to previous evaluations in variables named _, _1, _23 etc
. %reset_selective should clear these though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With