JuliaInterop / MATLAB.jl

Calling MATLAB in Julia through MATLAB Engine
MIT License
269 stars 63 forks source link

How to shut down engine on Windows 10? #166

Open ronubi opened 4 years ago

ronubi commented 4 years ago

I have tried several different methods to shut down the Matlab engine, before logging out of a Jupyter notebook running a Julia 1.3 kernel in Firefox on a Windows-10 box. (I never explicitly launch the engine, just use mat"" commands.) When I restart the notebook (and the Julia kernel), then import MATLAB.jl, my Matlab workspace variables and path from the previous session are all still there. And Windows TaskManager shows a MATLAB process running. Of course I can just use TaskManager to "End task" - but that seems rather indirect.

The only way I have found to shut down the engine within the notebook is to do

MATLAB.mat"quit"

which results in

MATLAB.MEngineError("invalid engine session (err = 1)")

followed by

MATLAB.close_default_msession()
MATLAB.restart_default_msession()

which crashes the Julia kernel. Sometimes the kernel restarts on its own, other times I need to use the menu Kernel->Restart.

Please advise.

musm commented 4 years ago

Have you tried MATLAB.close_default_msession() ? What's the error in that case? It works for me from a terminal, not sure why it wouldn't when run form Jupyter.

ronubi commented 4 years ago

Calling close_default_msession() seems to work, but it doesn't accomplish anything. If I then call mat"whos", I still see all the variables I had in the previous session! The same thing happens if I also call restart_default_msession()! It's like these functions are no-ops.

ronubi commented 4 years ago

I just tested MATLAB.jl in a simple Julia REPL session. Similarly, close_default_msession() seems to be a no-op. However, when I quit the REPL (Ctrl-D), it does shut down the engine (and the Windows Matlab process). So the problem is that logging out of the Jupyter notebook session and shutting down the server (Ctrl-C & 'Y' for Yes) is not sufficient to kill the Matlab engine/process. I guess I'll follow up my Jupyter-notebook shutdown sequence with TaskManager Processes -> right-click on Matlab -> End-task, unless someone has a better idea.

(FYI, I always start the REPL or the Jupyter notebook from bash [MinGw on Windows].)

vxj9800 commented 2 years ago

I have recently started using Julia with the MATLAB package. I am having similar issues where the pointer to the MATLAB session in Julia is unusable when close(s) is executed in REPL. However, the MATLAB session is still active (visible in task manager and still using resources, mostly RAM) and doesn't close until REPL is closed. I am using this package to access a toolbox in MATLAB. The real issue for me is that the computation I do require large amount of memory. So, about 5GB of memory stays allocated to the MATLAB session. I would love to have a solution for this issue so that I can make better use of the memory.