MATLAB: Limiting MATLAB memory usage

memory

I am running MATLAB on 64-bit Win7. When I type "memory" I get:
Maximum possible array: 19851 MB (2.082e+010 bytes) *
Memory available for all arrays: 19851 MB (2.082e+010 bytes) *
Memory used by MATLAB: 535 MB (5.613e+008 bytes)
Physical Memory (RAM): 12286 MB (1.288e+010 bytes)
* Limited by System Memory (physical + swap file) available.
As you can see, this suggests MATLAB is given ~20 GB to create arrays while I have only ~12 GB of memory.
What happens is that in some cases while developing code, MATLAB tries to create arrays that are larger than 12 GB. This usually just freezes my system and is a big annoyance.
I would much rather prefer MATLAB just give me an "Out of memory" error if it exceeds like 4 or 8 GB of memory usage or something. I haven't been able to figure out how to do this. Is there a way to limit how much memory MATLAB can use? All the searches I do just seem to be answers about how to avoid the "Out of memory" errors…
Thanks.

Best Answer

If you are using a sufficiently recent release of MATLAB (release R2015a or later) take a look at the Array Size Limit item in the Workspace And Variables section in the Preferences for MATLAB.
Related Question