Memory allocated when creating Python objects is never automatically freed
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- performance
Direzione di ricerca
Inizia eseguendo il createmodel() CP-SAT MWE fornito con PythonCall e CondaPkg, quindi confronta gli effetti di PythonCall.pydel!(...) e GC.gc(). Traccia i punti di ingresso della durata degli oggetti e del garbage collection di PythonCall; il lavoro è completato quando chiamate ripetute rilasciano le allocazioni fuori ambito senza richiedere GC manuale.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Julia
- Stelle
- 1.1k
- Fork
- 89
- Merge medio
- 1g 22h
- PR unite (30g)
- 5
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JuliaPy/PythonCall.jl
-
Python 3.15 from conda-forge: "Could not find Python library" because libpython is not installedAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
JuliaPy/PythonCall.jl#838 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
JuliaPy/PythonCall.jl#828 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
JuliaPy/PythonCall.jl#809 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Locking julia dependenciesApertaenhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
JuliaPy/PythonCall.jl#805 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
JuliaPy/PythonCall.jl#796 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di JuliaPy/PythonCall.jl
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
SciML/PETScDiffEq.jl#196 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 84/100
SciML/Catalyst.jl#1571 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
SciML/Optimization.jl#1414 ·
I maintainer di solito rispondono entro 1 giorno