Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deallocate memory from an object in Jupyter Notebook

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()
  1. 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)

like image 315
Thalish Sajeed Avatar asked Mar 18 '19 12:03

Thalish Sajeed


People also ask

How do you clear memory on a Jupyter notebook?

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.

How do I delete a variable from memory in Jupyter notebook?

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 .

Which is the correct way to deallocate memory in Python?

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.

How do you clear the output of a cell in Jupyter notebook?

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.


1 Answers

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.

like image 75
Mihai Andrei Avatar answered Nov 08 '22 23:11

Mihai Andrei