MATLAB: Does MATLAB crash if the MATLAB workspace takes up more than half of the total Linux memory and I issue a shell command from the MATLAB command line

MATLAB

If my MATLAB workspace takes up more than half of my total Linux memory (Physical + Swap), and I issue a shell command (such as !ls) from the MATLAB command line, MATLAB crashes. The crash happens only when I issue a shell command after allocating MATLAB workspace. However, if I issue a shell command first, then allocate more than half of the available memory to MATLAB workspace and then issue a shell command, MATLAB does not crash.

Best Answer

This behavior is exhibited because on unix when you fork a process, you initially make a complete logical copy of the initial process. This is mostly entries in the process page map that are never going to get used (we will almost immediately call exec to run ls executable) but the OS tries to allocate enough resources to allow the forked process to run correctly if it did not call exec. The fork fails due to lack of memory.
The reason the failure occurs only when invoking a shell command after allocating memory is that MATLAB forks a helper process to mediate interaction with the shell; this process is forked the first time a shell command is invoked. If a command is invoked when MATLAB memory utilization is low, no resource limits are encountered.
Possible Workaround:
1. Configure the system with a lot more swap space and then set the resource limits appropriately. This won't hurt performance since we will immediately call exec and we never page in massive MATLAB child.
2. Add
"!true;"
to "startup.m" file. This still causes matlab_helper to be started, but produces no output. In fact, the sole purpose of "true" is to return the exit code 0. The only impact would be MATLAB's startup will take a bit longer.