How to limit RAM memory usage for compiled matlab executables?

23 views (last 30 days)
I use Matlab 2022b and the corresponding MCR2022b
Even for the following min. example the RAM-memory usage after startup is about 300MB or above. After some time it reduces to a quite lower amount.
Min bspw:
File:
%simple example for memory alloc as executable
j=0;
for jj=1:1e8
j=j+1;
pause(10)
disp(j);
end
Code for console:
mcc('-m', 'my_simple_exe_code.m', '-d', pwd, '-o', 'my_simple_exe_code')
For my real code problem, the numbers of RAM-memory usage are as follows:
After startup as .exe: 400-600MB
After 3h of running of .exe: 30-70MB
Running code in Matlab: 30-70MB of additional RAM-usage.
I already tried to set some additional options for the compiler
opts = compiler.build.StandaloneApplicationOptions([file_name,'.m'],...
'ExecutableName',exe_name,...
'OutputDir' ,deploy_path,...
'SupportPackages',{'none'}...
);
compiler.build.standaloneApplication(opts);
But this does not change anything.
As far is i understood matlab runtime load a number of functions into the memory. After the process is running stable it checks which functions are required at all and reduces the number of loaded functions. Is there a possibility to check the required packages/functions earlier?

Answers (1)

Aditya
Aditya 4 minutes ago
Hi Andreas,
You've observed a common behavior with MATLAB compiled executables (using MCR): high initial memory usage that drops after some time. This is due to the way the MATLAB Runtime (MCR) manages its internal resources.These are the reasons for why it happens.
  • Startup Overhead: When your .exe starts, the MCR loads a large set of libraries, toolboxes, and internal data structures into memory, regardless of whether your code uses them.
  • Deferred Unloading: After some idle time or after the JIT (just-in-time) compiler and garbage collector run, the MCR may unload unused functions/packages, reducing memory usage.
  • Dynamic Analysis: The MCR determines which functions are actually needed as your program runs, and may free up memory for unused components later.
There is no public or documented API in MATLAB or the MCR to force early unloading of unused functions or packages. The memory management is internal to the MCR and not user-controllable.You can try to minimize what is bundled with your executable, but the MCR itself is still a large runtime.Recommendations:
  • Minimize dependencies:Use only core MATLAB functions in your code. Avoid toolboxes if not needed.
  • Strip support packages:You already use 'SupportPackages',{'none'}—this is good.
  • Profile your code:Use dependencyReport or matlab.codetools.requiredFilesAndProducts to see what your code actually uses, and remove unnecessary dependencies.
You can refer to the following documentation for more details.
https://www.mathworks.com/help/compiler/

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!