Memory leak when interfacing between Matlab and python
古いコメントを表示
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 件のコメント
Rakshit Kothari
2023 年 1 月 16 日
I am observing the same issue with MATLAB R2022b with Python running on Ubuntu. Any help would be appreciated! Likewise, I'll keep this thread posted if I find a solution or workaround.
Walter
2023 年 1 月 17 日
We are as well. Same matlab version.
Rakshit Kothari
2023 年 1 月 18 日
For what it's worth, I find that initializing MATLAB via Python as such `eng = matlab.engine.start_matlab('-nodisplay -singleCompThread -nojvm')` helps reduce MATLAB's memory consumption but the leak still exists. Hopefully you can finish your job with the saved memory and avoid waiting for an actual solution.
Rakshit Kothari
2023 年 1 月 18 日
I can also confirm that this issue does not occur in R2021b.
hyuntae.kim
2023 年 3 月 7 日
I am using a python open module from matlab as a blockproc function.
MATLAB version is R2021b R2022b.
1. I confirmed that the MATLAB process memory and analysis time increased as image analysis was repeated using blockproc().
2. Delete and reload the Python environment with the code below
>> terminate(pyenv); py. list;
3. I checked the task manager.
4. In the MATLAB R2022b version, the memory increased compared to when the Python open module was first called.
5. I have to quit and restart MATLAB R2022b to get the memory back.
涵
2023 年 7 月 9 日
- I have the same problem when using R2022b python API in Ubuntu20.02.
- I solved this problem by save the actual parameter which is fed to eng.my_function(actual parameter) in .mat format, and then load them in my_functionI.m.
- I'm pretty sure the problem is caused by trying to transmit parameter from matlab python API(eng.function(actual parameter)) to matlab engine. So try to avoide transmit parameter or transmit it like 2.
- Additionally, if you just transmit a short string, there are also no memory leaks.
Shisui Uchiha
2023 年 9 月 1 日
I have this problem with matlab R2023a in Ubuntu22.04 too...
Shisui Uchiha
2023 年 9 月 1 日
編集済み: Walter Roberson
2023 年 11 月 24 日
Øyvind
2023 年 11 月 24 日
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 )
回答 (3 件)
Donato
2023 年 8 月 16 日
0 投票
I am facing the same problem, we really need some support here.
Clark
2023 年 12 月 28 日
0 投票
Me too! HUGE leaks... Sometimes swelling to 40 GB, when expecting maybe only 2GB
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.
カテゴリ
ヘルプ センター および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!