Memory allocated when creating Python objects is never automatically freed
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python
- 领域
- performance
调研方向
首先使用 PythonCall 和 CondaPkg 运行提供的 createmodel() CP-SAT MWE,然后比较 PythonCall.pydel!(...) 和 GC.gc() 的影响。跟踪 PythonCall 的对象生命周期和垃圾回收入口点;完成标准是重复调用能够释放超出作用域的分配,而不需要手动 GC。
由索引模型根据 Issue 内容生成。
描述
Affects: PythonCall
Describe the bug
I am trying to use PythonCall.jl to call the Python interface to CP-SAT, which is a popular constraint programming library from Google's OR-Tools toolkit. The problem is that the memory that gets allocated when creating a Python object from that interface is never automatically freed.
A function createmodel() is used to create a CpModel object`, which goes out of scope after the function terminates. Each time the function is executed, the memory gets fuller, until eventually the Julia process gets killed. The expected behavior is that at some point before Julia gets killed, the memory that was allocated for objects that are out of scope should get released.
Here are the steps to reproduce a MWE, assuming the environment already has PythonCall.jl and CondaPkg.jl:
Add OR-Tools to the environment and import it:
using PythonCall, CondaPkg
] conda pip_add ortools
cp_model = pyimport("ortools.sat.python.cp_model")
Define the function that will create and initialize a CpModel object:
createmodel() = let
model = cp_model.CpModel() # create model
variables = [model.new_int_var(0,1, "x_" * string(i)) for i in 1:100] # add variables to the model
model.add_allowed_assignments(variables, [rand(0:1, 100) for _ in 1:200000]) # add constraints over those variables
return nothing
end
Repeatedly call createmodel() and observe that the memory usage keeps going up until the Julia process gets killed.
createmodel()
createmodel()
createmodel()
# ... if you keep calling createmodel(), eventually the memory gets full
Your system
Please provide detailed information about your system:
-
Operating System: Fedora Linux 43
-
Python version: 3.14.3
-
versioninfo()Julia Version 1.12.3 Commit 966d0af0fdf (2025-12-15 11:20 UTC) Build Info: Official https://julialang.org release Platform Info: OS: Linux (x86_64-linux-gnu) CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz WORD_SIZE: 64 LLVM: libLLVM-18.1.7 (ORCJIT, tigerlake) GC: Built with stock GC Threads: 1 default, 1 interactive, 1 GC (on 8 virtual cores) -
import Pkg; Pkg.status()Status `~/MWECPSAT/Project.toml` [992eb4ea] CondaPkg v0.2.34 [6099a3de] PythonCall v0.9.31 -
import CondaPkg; CondaPkg.status()Environment /home/dmaioli/MWECPSAT/.CondaPkg/.pixi/envs/default Pip Packages ortools v9.15.6755
Additional context
- Calling
PythonCall.pydel!(model)and/orfor v in variables PythonCall.pydel!(v) endinside the body of the function does not have any visible effect - Executing
GC.gc()after the function calls has the effect that at least part of the allocated memory gets freed, but sometimes this does not happen if it is executed only once. - Calling the equivalent code directly from Python works as expected, i.e. the memory gets freed after the function terminates. Here is the Python version:
from ortools.sat.python import cp_model import randomdef createmodel(): model = cp_model.CpModel() variables = [model.new_int_var(0,1, f"x_{i}") for i in range(100)] model.add_allowed_assignments(variables, [[random.randint(0,1) for _ in range(100)] for _ in range(200000)]) return Nonecreatemodel() createmodel() createmodel() # ... if you keep calling createmodel(), the memory never gets full
- 主要语言
- Julia
- 星标
- 1.1k
- 派生
- 89
- 平均合并
- 2 天 15 小时
- 30 天内合并 PR
- 7
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JuliaPy/PythonCall.jl 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 55/100
JuliaPy/PythonCall.jl#828 · 7 条评论 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 48/100
JuliaPy/PythonCall.jl#809 · 2 条评论 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
JuliaPy/PythonCall.jl#805 · 4 条评论 ·
维护者通常 1 天内回复
-
enhancement
难度 3/5 1-2 天 新手友好度 68/100
JuliaPy/PythonCall.jl#796 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 25/100
JuliaPy/PythonCall.jl#790 ·
维护者通常 1 天内回复
查看 JuliaPy/PythonCall.jl 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
oxfordcontrol/COSMO.jl#211 ·
-
documentation
难度 2/5 半天 新手友好度 65/100
维护者通常 6 天内回复
-
Out-of-place JLArray/GPU problem with VectorContinuousCallback scalar-indexes (callback cache built with CPU zeros)可能已有人在做 @ChrisRackauckas-Claude 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
SciML/OrdinaryDiffEq.jl#4813 ·
维护者通常 1 天内回复
-
ARKODE: callbacks that modify `u` throw MethodError on reinit可能已有人在做 @devmotion 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 79/100
SciML/Sundials.jl#575 ·
-
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复