Memory leak when interfacing between Matlab and python

9 vues (au cours des 30 derniers jours)
Surat Asvapoositkul
Surat Asvapoositkul le 1 Nov 2019
Modifié(e) : Neil le 9 Oct 2024
Hi. I have found a problem when I try to make communication between Matlab and python (using optimisation in matlab). I have built a model in Matlab and tried to use Scipy to optimise the parameters in the model. Therefore, I wrote an objective function in python and use "matlab.engine" to interface between the two. However, I have found that the simulation time in each iteration increases when I call Matlab engine.
With the code below, the simulation time keeps increasing after finishing eacg interation.
eng = matlab.engine.start_matlab()
def opt_pf(x):
y = eng.matlab_model(excel_path, x, nargout=0)
return y
gc.collect()
### Run optimisation
from scipy.optimize import minimize
res = minimize(opt_pf, x0, method='SLSQP', constraints=[eq_cons], options={'ftol': 1e-09, 'disp': True}, bounds=bounds)
I think that the optimizer canno't clear the cache from matlab.engine in each iteration.
  9 commentaires
Øyvind
Øyvind le 24 Nov 2023
I have experienced similar issues (Matlab R2023b on Windows).
For me, it seems like the problem is mostly if running in ExecutionMode="OutOfProcess", while "InProcess" is better.
With OutOfProcess execution mode, I see that there is a process called matlabpyhost which ends up using tons of memory (as seen in Task Manager).
So to do this, use something like:
pyenv(ExecutionMode="OutOfProcess")
(Obviously, the InProcess vs OutOfProcess also affects other things, see https://se.mathworks.com/help/matlab/ref/pyenv.html )

Connectez-vous pour commenter.

Réponses (3)

Donato
Donato le 16 Août 2023
I am facing the same problem, we really need some support here.

Clark
Clark le 28 Déc 2023
Me too! HUGE leaks... Sometimes swelling to 40 GB, when expecting maybe only 2GB

Neil
Neil le 9 Oct 2024
Modifié(e) : Neil le 9 Oct 2024
I encountered the same issue in MATLAB2023b. The attachments in the following old bug report looks helpful, but it is for a much much older version of MATLAB.

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by