在循环中调用get_window_extent()
以使包含MathText的Text对象增加了使用的内存,尽管已删除了Text对象。最小的工作示例:
import matplotlib.pyplot as plt
import random
fig = plt.figure()
renderer = fig.canvas.get_renderer()
sample_text = ' '.join("${}$".format(random.random())
for _ in range(1000))
while True:
# This wouldn't make memory usage grow:
# text = sample_text
# This wouldn't either:
# text = ' '.join("{}".format(random.random())
# for _ in range(1000))
# But this does:
text = ' '.join("${}$".format(random.random())
for _ in range(1000))
# ...when combined with get_window_extent():
plotted_text = fig.text(0, 0, text)
plotted_text.get_window_extent(renderer)
plotted_text.remove()
任何想法为何以及如何预防?