Python is an excellent programming language to quickly develop small applications. However it has substantial issues in terms of memory management and of speed. One particular inefficiency concerns the creation of a large quantities of images with for example the following code,
import numpy as np
import matplotlib.pyplot as plt
Niter=10000
for i in range(Niter):
fig, ax = plt.subplots(1, figsize=(12, 6))
ax.plot(np.random.normal(1, 10, 100))
fig.savefig('fig_' + str(i) + '.jpg', dpi=300)
plt.close()
Despite a plt.close()
, you may saturate the memory of your system and eventually python will crash.
What is the problem here? Python incrementally index figures and still keep them in memory. If you want to completely erase from memory the figure, you need the following modification,
import numpy as np
import matplotlib.pyplot as plt
Niter=10000
for i in range(Niter):
fig, ax = plt.subplots(1, figsize=(12, 6), num=1, clear=True)
ax.plot(np.random.normal(1, 10, 100))
fig.savefig('fig_' + str(i) + '.jpg', dpi=300)
We have added num=1, clear=True
as arguments to the subplots function. This imposes to Python to overwrite the figure storage num=1
with the new plot after having erased it (clear=True
). Now, you will not experience any more any exponential growth of the memory usage of Python due to image generation.