Find and delete memory-hungry variables on Jupyter (IPython)-Qiita Following, the second in the "Memory release with python" series.
Target the list / array to find and delete variables that are occupying memory.
Define the following function.
def print_varsize():
import types
print("{}{: >15}{}{: >10}{}".format('|','Variable Name','|',' Size','|'))
print(" -------------------------- ")
for k, v in globals().items():
if hasattr(v, 'size') and not k.startswith('_') and not isinstance(v,types.ModuleType):
print("{}{: >15}{}{: >10}{}".format('|',k,'|',str(v.size),'|'))
elif hasattr(v, '__len__') and not k.startswith('_') and not isinstance(v,types.ModuleType):
print("{}{: >15}{}{: >10}{}".format('|',k,'|',str(len(v)),'|'))
What you are doing is simple "From the global variables, only the variables that have the size attribute or \ _ \ _ len \ _ \ _ attribute are extracted, and the name and size or len of that variable are output." Only. (Variables that start with'_' or whose type is module are excluded from print.)
>>> import numpy as np
>>> lst = [3,1,4,1,5]
>>> tpl = (1,5,3)
>>> a=np.zeros((100,100,3))
>>> print_varsize()
| Variable Name| Size|
--------------------------
| lst| 5|
| tpl| 3|
| Out| 0|
| a| 30000|
| In| 8|
In this way, large lists and arrays are obvious.
If you can do it so far,
del a
Just specify the variables you don't need and delete them. You can free up memory and continue to operate comfortably on Python.
Recommended Posts