Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Memory leak with pyplot

未关闭
#1,019 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
julia, python
领域
tooling

调研方向

从提供的 Julia MWE 和对 pyplot 的 PyCall 调用开始,比较创建绘图和进行垃圾回收与不创建绘图和不进行垃圾回收时的内存使用情况。通过 PyCall 跟踪返回的 fig 和 plot 对象;完成的标准是识别并修复被保留的引用,使重复绘图不再导致内存无限增长。

由索引模型根据 Issue 内容生成。

描述

When I create matplotlib.pyplot plots, the memory keeps increasing and is not released by the garbage collector. The following is the case I'm interested in (with subplots) and bellow I include a more basic MWE.

Example with subplots
using PyCall
plt = pyimport("matplotlib.pyplot")

function test_pyplot()
    for i in 1:10000
        @info i
        z = rand(4, 50)
        ẑ = rand(4, 50)
        fig, axs = plot_pyplot(z, ẑ)
    end
end

function plot_pyplot(z, ẑ)
    # plt = pyimport("matplotlib.pyplot")
    N = Int(size(z, 1)/2)
    fig, axs = plt.subplots(2, N)
    for i in 1:N
        axs[1,i].plot(z[i,:], label = "true")
        axs[1,i].plot(ẑ[i,:], label = "model")

        axs[2,i].plot(z[i+N,:], label = "true")
        axs[2,i].plot(ẑ[i+N,:], label = "predicted")

        axs[2,i].set_xlabel("time steps")
        axs[i].grid(true)
        axs[i+N].grid(true)

        axs[i+N].label_outer()
        axs[i].label_outer()
    end
    # plt.close()
    return fig, axs
end

test_pyplot()

I tried defining plt and closing it inside the plot_pyplot function (commented lines) but it doesn't solve the memory leak.

Here you have a simpler MWE:

using PyCall
plt = pyimport("matplotlib.pyplot")

plot_pyplot(z) = plt.plot(z)

function test_pyplot()
    for i in 1:1000000
        @info i
        z = rand(50)
        fig = plot_pyplot(z)
    end
end
Version info and status
julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.3.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 1 on 6 virtual cores
(memory_leak) pkg> st
Status `~/Documents/issues/memory_leak/Project.toml`
  [438e738f] PyCall v1.94.1

I need to use pyplot directly from PyCall.jl and not through PyPlot.jl because I need to pass the fig object to Wandb.jl. Actually, maybe I could get that object from PyPlot.jl, I didn't try that yet. But since I found the bug here, I think it can be helpful to report it anyway.

主要语言
Julia
星标
1.5k
派生
186
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaPy/PyCall.jl 的其他 Issue

查看 JuliaPy/PyCall.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。