JuliaPy / JuliaPy/PythonCall.jl

Memory leak when calling certain python packages from Julia via PythonCall

未关闭
#759 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Julia
星标
1.1k
派生
86
平均合并
1 天 22 小时
30 天内合并 PR
3

描述

Affects: PythonCall

Describe the bug

Memory is not released when repeatedly creating and closing matplotlib figures via PythonCall. I originally had the issue with an elaborate cartopy plotting loop, but have managed to recreate it in a simple while true loop MWE. Each iteration allocates a new figure/axes via plt.subplots, immediately closes it with plt.close, calls PythonCall.pydel! on both the figure and axes objects, and calls GC.gc(). However, there is still a leak until the process is killed.

This does not occur when repeatedly allocating and deleting a numpy array in an equivalent loop, so the issue has something to do with what kind of python objects are created. I also have similar memory leak issues with scikit-learn.

The issue is reproducable both in the Julia REPL and in the VS Code Julia extension, and when I execute a script containing the below code from the command line.

MWE:
using PythonCall
@py import matplotlib.pyplot as plt

i = 0
while true
global i += 1
fig, ax = plt.subplots(1, 1)
plt.close(fig)
PythonCall.pydel!(ax)
PythonCall.pydel!(fig)
GC.gc()
println("Iteration $i done")
end

My system

OS: macOS 15.7.4 (arm64, Apple Silicon)
Julia: 1.12.6
Python: 3.14.2 (conda-forge, Clang 20.1.8)
PythonCall: 0.9.31
matplotlib: 3.10.8

Thanks for your help!

贡献指南

这个仓库没有索引到贡献指南

调研方向

Start by running the supplied PythonCall and matplotlib MWE with the listed Julia, Python, and package versions, then compare its memory behavior with the numpy loop. Investigate why closing and deleting the figure and axes does not release memory; done means the repeated loop no longer grows indefinitely and the behavior is verified in the REPL and command-line script.

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

评估

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

把新 issue 发到你的邮箱

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