MATLAB doesn't exit, all windows close, but MATLAB.exe persists in Task Manager
Show older comments
Well, the title says it all. Is this a common problem? What could cause and fix this?
I'm on Win8x64 and I use R2014b. When I quit MATLAB, either by pressing the cross or typing
exit
in the command window, all open MATLAB windows close after a few seconds, but the process remains in the Task Manager. It takes a lot of memory too: the usual amount MATLAB uses (usually over 500MB). When I launch MATLAB again, I get a second MATLAB.exe and the first one remains unchanged. I can kill the process by force closing it in the Task Manager, but I'm not comfortable with doing that often and the extra clicks are just annoying.
Answers (1)
Debarati Banerjee
on 13 Apr 2015
Edited: Debarati Banerjee
on 13 Apr 2015
It is a known issue with MATLAB R2014b. As a workaround MATLAB can me made to terminate itself via a call to the operating system. You can place the following lines of code in a script named "finish.m" and place the script on the MATLAB path. The codes should be as follows:
id = feature('getpid');
if ispc
cmd = sprintf('Taskkill /PID %d /F',id);
elseif (ismac || isunix)
cmd = sprintf('kill -9 %d',id);
else
disp('unknown operating system');
end
system(cmd);
12 Comments
Al in St. Louis
on 20 Sep 2019
This is still needed with R2018b.
Palash Sarate
on 19 Nov 2020
still needed in year 2020
Jiri Junek
on 16 Dec 2020
i have same problem in 2020b
SYEDNOOR S
on 26 Apr 2021
I am facing the same issue with MATLAB R2020b. both exit and quit(0,'force') as well does not work.
SYEDNOOR S
on 26 Apr 2021
Edited: SYEDNOOR S
on 26 Apr 2021
@Debarati Banerjee: The solution works well to terminate the MATLAB application, however if we use your solution in windows command line as below
matlab --nodesktop -nosplash -minimize -wait -log -r "retCode= my_mscript_fun( ); disp('done!'); finish(); quit(retCode)"
MATLAB returns the following error
ERROR: MATLAB error Exit Status: 0x00000001
How can we avoid this error, in order to use the solution better.
Raphael
on 3 May 2021
From my perspective, this is a serious bug and known issues since 7 years and still not yet fixed. Consider, this is a blocker for each realiable execution of MATLAB/SIMULINK on a CI platform. I cannot understand that for such expensive software.
We have that issue with 2018b and Windows Server 2016. When we open a Simulink model, the application does not exit properly. No chance to fix that from our site. Nothing helped as also mentioned by the other comments. Moreover, the above fix is not acceptable, since the application returns an error. Or to be more precise, above proposal is not a Windows compliant way to close an application. Please fix that bug in your software and not by weird work-arounds.
Keith Myescough
on 7 Jul 2022
Edited: Keith Myescough
on 7 Jul 2022
Another year on, still suffering from this issue. Our CI/CD implementation is failing because of this. Really frustrating after all the effort we put into getting our unit tests to run on the build server to discover that Matlab can't properly close itself. I would appreciate a pointer to the issue in the list of known bugs, then we can track the progress.
FYI: for us the issue only occurs in tests that require a plot function, but that is most of them...
Keith Myescough
on 12 Jul 2022
Edited: Keith Myescough
on 12 Jul 2022
For anyone else suffering from this issue, I have a workaround for a bat step in CI/CD in conjunction with the workaround posted by Debarati above:
"C:\\Program Files\\MATLAB\\R2020b\\bin\\win64\\MATLAB.exe" ^
-batch ^
"val = 21; fid=fopen('return.txt', 'w'), fprintf(fid, '%%d', val), fclose(fid), quit(val)"
SET /p Val=<return.txt
IF /i "%Val%" == "0" (
echo Unit tests were executed successfully
) ELSE (
echo Unit test failed, return code %Val%
exit /b 1
)
Where the val = 21 should be replaced by some form of unit test output, for proper functionality. I spent only a little effort in finding out if I could split the length Matlab command onto multiple lines, but this was the best I could do.
Tim Darrah
on 18 Jan 2023
Still a problem in 2022a!
The command
matlab -nodesktop -nodisplay -nosplash -logfile $DATE\_run\_$i.log -batch "try, run('simulation.m'), catch e, fprintf('%s', string(e.message)), end; exit;"
does not exit, even when "simulation.m" has finished running.
Well said @Raphael
"From my perspective, this is a serious bug and known issues since 7 years and still not yet fixed. Consider, this is a blocker for each realiable execution of MATLAB/SIMULINK on a CI platform. I cannot understand that for such expensive software."
This is an issue when using both the matlab gui as well as from the command line. Literally, @MathWorks Support Team, we just need matlab to quit when we say quit. Not have our windows close but then matlab utilize 100% of cpu until it is forcefully killed, not have a blinking cursor hang in a terminal and have matlab utilize 100% of cpu until it is forcefully killed.
Milo Fryling
on 1 Feb 2023
It's a showstopper for us in our lab. I isolated a line of code, where MATLAB process hangs around after termination if it is present, and not if it is removed. The author of the code says, " It looks like the issue relates to MEX and/or CUDA. The function you have isolated is the first time MATLAB calls compiled code...My first guess is that we somehow need to close the CUDA context that is created when you run GPU code."
The line of code is:
Yb = cyrebin( Y(:), Y_B, int_params_b, double_params_b, GPU_idx, det_type ); % , proj_time );
Preetham Manjunatha
on 29 Jun 2025
Same issue in R2025a!
Dyuman Joshi
on 29 Jun 2025
@Preetham Manjunatha, you should report this to TMW - Contact Support > Product Usage > Report a Bug
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!