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.
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
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).
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.
Recommended Posts