ManageMATLABResources in JVM
MATLAB®Compiler SDK™uses a Java Native Interface (JNI) wrapper connecting your Java®application to the C++MATLAB Runtime. As a result, most of the resources consumed by theMATLAB Compiler SDKportions of your Java application are created byMATLAB Runtime. Resources created byMATLAB Runtimeare not visible to the JVM™. The JVM garbage collector cannot effectively manage resources that it cannot see.
All of theMATLAB Compiler SDKJava classes have hooks that free MATLAB resources when the JVM garbage collector collects the wrapper objects. However, JVM garbage collection is unreliable because the JVM sees only the small wrapper object. The garbage collector can forgo spending CPU cycles to delete the small wrapper object. Until the Java wrapper object is deleted, the resources allocated inMATLAB Runtimeare also not deleted. This behavior can result in conditions that look like memory leaks and rapidly consume resources.
To avoid this situation:
Never create anonymous MATLAB objects.
Always dispose of MATLAB objects using their
处理()
方法。
For information about the interaction between the interface for MATLAB and Java and the JVM, seeInteraction Between MATLAB Compiler SDK and JVM.
NameMATLAB对象Resource Maintenance
All of the MATLAB objects supported byMATLAB Compiler SDKhave standard Java constructors as described in the Java API documentation in
.matlabroot
/help/toolbox/javabuilder/MWArrayAPI
When creating MATLAB objects, always assign them names. For example, create a 5-by-5 cell array.
MWCellArray myCA = new MWCellArray(5, 5);
The Java objectmyCA
is a wrapper that points to a 5-by-5mxCellArray
object inMATLAB Runtime.myCA
can be added to other MATLAB arrays or manipulated in your Java application. When you are finished withmyCA
, you can clean up the 5-by-5mxCellArray
by using the object’s处理()
方法。
The semantics of the API allows you create anonymous MATLAB objects and store them in named MATLAB objects, but you shouldneverdo this in practice. You have no way to manage the MATLAB resources created by the anonymous MATLAB object.
Consider the following code that creates a MATLAB array, data, and populates it with an anonymous MATLAB object:
MWStructArray data = new MWStructArray(1, KMAX, FIELDS); data.set(FIELDS[0], k + 1, new MWNumericArray(k * 1.13));
Two MATLAB objects are created. Both objects have a Java wrapper and a MATLAB array object inMATLAB Runtime. When you dispose ofdata
, all of the resources for it are cleaned up. However, the anonymous object created bynew MWNumericArray(k * 1.13)
is just marked for deletion by the JVM. Because the Java wrapper consumes a tiny amount of space, the garbage collector is likely to leave it around. Since the JVM never cleans up the wrapper object,MATLAB Runtimenever cleans up the resources it has allocated.
Now consider the following code, where the MATLAB object’sset()
methods accept native Java types:
MWStructArray data = new MWStructArray(1, KMAX, FIELDS); data.set(FIELDS[0], k + 1, k * 1.13);
In this instance, only one MATLAB object is created. When its处理()
method is called, all of the resources are cleaned up.
Release Resources ofMATLABObjects
Clean up MATLAB objects by using the:
Object’s
处理()
methodStatic
MWArray.disposeArray()
method
Both methods release all of the resources associated with the MATLAB object. The Java wrapper object is deleted. If there are no other references to theMATLAB RuntimemxArray
object, it is also deleted.
The following code disposes of a MATLAB object using its处理()
方法。
MWCellArray myCA = new MWCellArray(5, 5); ... myCA.dispose();
The following code disposes of a MATLAB object using theMWArray.disposeArray()
方法。
MWCellArray myCA = new MWCellArray(5, 5); ... MWArray.disposeArray(myCA);