The reason for MATLAB swapping prematurely could be attributed to a combination of the high memory footprint required by the Parallel Computing Toolbox (PCT) and how the operating system handles parallelism. In this case, we do not have access to, nor can we test on a dual-processor Mac machine. However, we can try to mitigate the high memory demands imposed by the PCT.
In general, PCT will always add further pressure to memory requirements. For example, consider a standard MATLAB process that uses 2GB of RAM, with a 1GB variable in the workspace, totaling approximately 3GB of memory. A user may then write a PCT algorithm that employs a pool of 12 workers.
- The 12 workers will start 12 extra MATLAB processes, each potentially using about 2GB of memory (+ 24GB)
- The algorithm may need to broadcast that 1GB variable to each worker MATLAB in the computation ( + 12GB)
- During data transfer, we can temporarily have two extra copies of the variable on both the sending and receiving sides. This contributes to a spike in memory usage, not the steady state usage. ( Worst case: +2GB + 12 * 2GB = 26GB)
Adding these all up gives a worst-case peak memory usage of 62GB. This spike in memory usage may cause MATLAB to swap to disk memory temporarily. The swap memory usage indicated by iStatPro may reflect the prior usage due to a spike in memory usage, hence it may imply that MATLAB begins to swap to disk memory when there's still RAM available.
In summary, each of the workers utilized by PCT will require sufficient memory to start/run a session of MATLAB, have a copy of the data the worker is running on, and accommodate any additional memory demanded by the algorithm.
- Lowering the number of workers on the machine will reduce the memory footprint. When using a process-based parallel pool, parpool("local") or parpool("Processes"), the "NumWorkers" controls how many processes are started. We generally advise that the number of workers should be fewer than the number of physical cores. \n
>> parpool("local");
>> parpool("Processes");
- If memory usage continues to be an issue, you may consider using a thread-based parallel pool by running parpool("threads"); this uses multiple MATLAB computational sessions inside a single process and often has a far lower memory footprint than the former option.
- More details about the differences between process-based parallel and thread-based parallel can be found here: