How to deal with memory leaks in matplotlib.pyplot

You can prevent memory leaks by creating and terminating the drawing process separately as shown below.

from multiprocessing import Pool
import matplotlib.pyplot as plt
import numpy as np

#Method for plotting
# plt.clf()・ Plt.close()The memory is automatically released when the process ends without doing this.
def plot(args):
    x, y = args
    plt.plot(x, y)

#Value to plot
x = np.arange(1e7)
y = np.arange(1e7)

#Create a process for plotting and perform drawing processing in it
p = Pool(1)
p.map(plot, [[x,y]])
p.close()

You can check if the memory is actually released with the following code.

from multiprocessing import Pool
import matplotlib.pyplot as plt
import numpy as np

#Method for plotting
# plt.clf()・ Plt.close()Memory is automatically released when the process ends without doing this.
def plot(args):
    
    x, y = args
    plt.plot(x, y)
    
    #You may do the following for verification
    plt.tight_layout()
    plt.savefig('aa.jpeg')

#Plot 10 times to see if memory usage changes
for i in range(10):
    
    #Value to plot
    x = np.arange(1e7)
    y = np.arange(1e7)
    
    #Create a process for plotting and perform drawing processing in it
    p = Pool(1)
    p.map(plot, [[x,y]])
    p.close()
    
    #Show memory usage
    import psutil
    mem = psutil.virtual_memory().free / 1e9
    print(i, f'memory used: {mem} [GB]')

By the way, a memory leak occurs in the following cases, for example. (Reference: Memory may not be released just by plt.close ()-Qiita)

--If you only do plt.close () --If you are doing plt.clf ()plt.close (), but you are doing plt.tight_layout () or plt.savefig ()

Recommended Posts

How to deal with memory leaks in matplotlib.pyplot
How to deal with run-time errors in subprocess.call
How to deal with pyenv initialization failure in fish 3.1.0
How to deal with Executing transaction: failed in Anaconda
How to deal with imbalanced data
How to deal with imbalanced data
How to deal with DistributionNotFound errors
[AWS] How to deal with "Invalid codepoint" error in CloudSearch
For beginners, how to deal with common errors in keras
How to work with BigQuery in Python
How to deal with enum compatibility errors
[Python] How to deal with module errors
How to deal with python installation error in pyenv (BUILD FAILED)
How to reduce GPU memory usage with Keras
How to deal with errors when hitting pip ②
[REAPER] How to play with Reascript in Python
How to implement shared memory in Python (mmap.mmap)
can't pickle annoy. How to deal with Annoy objects
How to deal with module'tensorflow' has no attribute'〇〇'
How to deal with SessionNotCreatedException when using Selenium
How to use tkinter with python in pyenv
How to deal with garbled characters in json of Django REST Framework
How to deal with old Python versions in Cloud9 made by others
How to deal with "No module named'〇〇'" error in Jupyter Notebook | Install with! Pip!
How to convert / restore a string with [] in python
How to do hash calculation with salt in Python
Explain in detail how to make sounds with python
How to deal with Django's Template Does Not Exist
How to do zero-padding in one line with OpenCV
How to run tests in bulk with Python unittest
How to load files in Google Drive with Google Colaboratory
How to access with cache when reading_json in pandas
[Python] How to deal with pandas read_html read error
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
Investigate memory leaks with objgraph
How to RDP with Fedora31
How to develop in Python
2 ways to deal with SessionNotCreatedException
How to Delete with SQLAlchemy?
How to embed multiple embeds in one message with Discord.py
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to output a document in pdf format with Sphinx
How to extract any appointment in Google Calendar with Python
How to check ORM behavior in one file with django
[Django] How to give input values in advance with ModelForm
How to manipulate the DOM in an iframe with Selenium
[Linux] How to deal with garbled characters when viewing files
A story about how to deal with the CORS problem
How to create dataframes and mess with elements in pandas
How to deal with UnicodeDecodeError when executing google image download
Try HeloWorld in your own language (with How to & code)
How to log in to AtCoder with Python and submit automatically
[VLC] How to deal with the problem that it is not in the foreground during playback
Check for memory leaks in Python
How to cancel RT with tweepy
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
Python: How to use async with