MATLAB: How to handle a TLC Compiler out of memory error upon generating code for a Simulink model on a newer version of MATLAB while it works fine on an older version

Embedded Coder

I have a Simulink model that builds with successful code generation in release R2009b. I have upgraded to the latest version and reuse the same model to generate code. However, I receive an 'OUT_OF_MEMORY' error reported by the TLC Compiler and the model encounters a build failure.
ERROR: ### Loading TLC function libraries
TLC Compiler encountered an OUT_OF_MEMORY condition. Attempting to clean up...
I have the following system configuration:
RAM: 4GB
Swap Virtual Memory: 2GB
I checked memory in use using Windows Task Manager utility, but the physical memory in use is 90% a the point of OUT_OF_MEMORY error. This effectively means, that 10% remaining RAM is still available for use.

Best Answer

Memory requirements of a system change between different releases of MATLAB. Mostly with additional features and toolboxes added to every release of MATLAB, the memory requirements increase. However, this doesn't directly affect the memory usage by a TLC compiler during the build phase.
In an scenario described above, the process can be investigated by studying the build process for the model to code conversion. The Simulink model prior to generation of code, undergoes several phases that include:
1. Data initialization
2. Intermediate representation of the model
3. TLC build
4. Final code generation
Each of the steps above require larger memory for a very large model. The strict requirement implicit to the process is a contiguous block of memory, however large the memory requirement is. Based on this reasoning, if the final TLC build phase does not have a contiguous memory space as required in the RAM, the TLC compiler throws the OUT_OF_MEMORY exception, further, aborting the model build process.
Some to this problem on a Windows 32 bit machine are listed below. Their detailed usage is described enlisted in the documentation page listed.
Techniques to counter TLC OUT_OF_MEMORY exception:
1. Use 3GB switch to allocate more memory for MATLAB process
2. Start MATLAB from the $MATLABROOT$\bin directory as opposed to $MATLABROOT$\bin\win32 directory
3. Start MATLAB using -nojvm and -no desktop option
4. If the model does not use any Java Virtual Machine call, set the Java Heap space to the minimum. This can be done in "Preferences->General->Java Heap Memory".
Please refer to the following link for 3GB switch: