Find memory-consuming lists / arrays on Python

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.

code

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

Execution result

>>> 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

Find memory-consuming lists / arrays on Python
Manipulating Python character lists (arrays)
Find files like find on linux in Python
Understand python lists, dictionaries, and so on.
twitter on python3
python on mac
Python on Windbg
Python conda on cygwin
Find the maximum Python
Install python on WSL
PyOpenGL setup on Python 3
Install Python on Pidora.
Install Scrapy on python3
Union Find on networkX
Memorize Python commentary 5 --Lists
Python3 | Lists, Tuples, Dictionaries
Install Python on Mac
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Python> Tuples versus Lists
Python lists, tuples, dictionaries
Installing pandas on python2.6
python basic on windows ②
Install python on windows
List find in Python
Install Python 2.7.3 on CentOS 5.4
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Install Python 3.6 on Docker
Set-enable Python virtualenv on Windows
Set up Python 3.4 on Ubuntu
Run Openpose on Python (Windows)
Install Python 3.8 on RHEL 8 (AppStream)
Use matplotlib on Ubuntu 12 & Python
Python on Ruby and angry Ruby on Python
Find the difference in Python
Install pygame on python3.4 on mac
Python + Kivy development on Windows
Install Python 3.8 on CentOS 7 (SCL)
Real-time graphs on Plotly (Python)
Made python available on macOS
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Put Python 3.x on Ubuntu
Fastest Python installation on Windows
[Python] Notes on data analysis
Build Python environment on Windows
python> Handling of 2D arrays
Run Tensorflow 2.x on Python 3.7
Find permutations / combinations in Python
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Install OpenCV on Ubuntu + python
Notes on installing Python on Mac
Run Python CGI on CORESERVER
Twitter posts on Python 3 etc.
Run unix command on python
Build python environment on windows
Python --Install MySQLDB on EC2
Introducing TensorFlow on Ubuntu + Python 2.7