Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Memory allocated when creating Python objects is never automatically freed

未關閉
#754 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
python
領域
performance

研究方向

先使用 PythonCall 和 CondaPkg 執行提供的 createmodel() CP-SAT MWE,然後比較 PythonCall.pydel!(...) 和 GC.gc() 的影響。追蹤 PythonCall 的物件生命週期與垃圾回收進入點;完成標準是重複呼叫能釋放超出作用域的配置,而不需要手動 GC。

由索引模型根據 Issue 內容生成。

描述

bug

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/or for v in variables PythonCall.pydel!(v) end inside 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 random
    
    def 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 None
    
    createmodel()
    createmodel()
    createmodel()
    # ... if you keep calling createmodel(), the memory never gets full
    
主要語言
Julia
星號
1.1k
分支
89
平均合併
2 天 15 小時
30 天內合併 PR
7

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

JuliaPy/PythonCall.jl 的其他 Issue

查看 JuliaPy/PythonCall.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。