Packages produced by MATLAB® Compiler SDK™ use Java® Native Interface (JNI) to interact with the MATLAB Runtime.
When the first MATLAB Compiler SDK object is instantiated:
Dependent MATLAB Compiler SDK classes are loaded.
A series of shared libraries forming the JNI bridge from the generated package to the MATLAB Runtime are loaded.
The MATLAB Runtime is initialized by creating
an instance of a C++ class called mcrInstance
.
The MATLAB-Java interface establishes a
connection to the JVM™ by calling the JNI method AttachCurrentThread
.
AttachCurrentThread
creates a class
loader that loads all classes needed by MATLAB code utilizing
the MATLAB-Java interface.
The MATLAB Runtime C++ core allocates resources for the arrays created by the Java API.
As you create MWArray
objects to interact
with the MATLAB Runtime, the JVM creates a wrapper object
for the MATLAB mxArray
object. The MATLAB Runtime C++
core allocates the actual resources to store the mxArray
object.
This has an impact on how the JVM frees up resources used by
your application. Most of the resources used when interacting with MATLAB are
created by the MATLAB Runtime C++ core. The JVM only knows
about the MATLAB resources through the JNI wrappers created for
them. Because of this, the JVM does not know the size of the
resources being consumed and cannot effectively manage them using
its built in garbage collector. The JVM also does not manage
the threads used by the MATLAB Runtime and cannot clean them up.
All of the MATLAB Compiler SDK classes have static methods to properly dispose of their resources. The disposal methods trigger the freeing of the underlying resources in the MATLAB Runtime C++ core. Not properly disposing of MATLAB Compiler SDK objects can result in unpredictable behavior and may look like your application has a memory leak.