Find and delete memory-hungry variables on Jupyter (IPython)

I want to check / delete variables that are eating memory

When performing data analysis etc. on Jupyter (IPython), data is accumulated more and more in memory, so you want to check the variables that are consuming memory. In such a case, if you execute the following command, the variables and the memory capacity of the variables will be displayed in a list.

Python2


import sys

print "{}{: >25}{}{: >10}{}".format('|','Variable Name','|','Memory','|')
print " ------------------------------------ "
for var_name in dir():
    if not var_name.startswith("_"):
        print "{}{: >25}{}{: >10}{}".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|')

Python3


import sys

print("{}{: >25}{}{: >10}{}".format('|','Variable Name','|','Memory','|'))
print(" ------------------------------------ ")
for var_name in dir():
    if not var_name.startswith("_"):
        print("{}{: >25}{}{: >10}{}".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))

The output result looks like the following.


スクリーンショット 2017-01-03 17.34.11.png * * *

However, with this command, the results of all variables defined on Jupyter will be output, so for example, if you want to extract only variables whose memory capacity is above a certain value, arrange as follows. To do.

Python2


import sys

print "{}{: >25}{}{: >10}{}".format('|','Variable Name','|','Memory','|')
print " ------------------------------------ "
for var_name in dir():
    if not var_name.startswith("_") and sys.getsizeof(eval(var_name)) > 10000: #Arrange only here
        print "{}{: >25}{}{: >10}{}".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|')

Python3


import sys

print("{}{: >25}{}{: >10}{}".format('|','Variable Name','|','Memory','|'))
print(" ------------------------------------ ")
for var_name in dir():
    if not var_name.startswith("_") and sys.getsizeof(eval(var_name)) > 10000: #Arrange only here
        print("{}{: >25}{}{: >10}{}".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))

Then


スクリーンショット 2017-01-03 17.39.00.png

As you can see, only variables with a large memory capacity can be extracted.

If you can do it so far,

del U_Global, V_Global

Just specify the variables you don't need and delete them. You can free up memory and continue analysis comfortably on Jupyter (IPython).

Remarks

The title says "on Jupyter (IPython)", but I think it will probably be displayed in the same way on other interfaces. However, since it uses str.format (), it must be Python 2.6 or higher for correct output.

Reference URL

Recommended Posts

Find and delete memory-hungry variables on Jupyter (IPython)
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Install Anaconda on Mac and upload Jupyter (IPython) notebook to Anaconda Cloud
Install matplotlib and display graph on Jupyter Notebook
Launch and use IPython notebook on the network
Golang on jupyter
Jupyter on AWS
Easily launch jupyter notebook on AWS and access locally
EC2 provisioning with Vagrant + Jupyter (IPython Notebook) on Docker
Encrypt and save data on jupyter and decrypt if necessary