Main Content

Manage MATLAB Resources 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 the MATLAB Compiler SDK portions of your Java application are created by MATLAB Runtime. Resources created by MATLAB Runtime are not visible to the JVM®. The JVM garbage collector cannot effectively manage resources that it cannot see.

All of the MATLAB Compiler SDK Java 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 in MATLAB Runtime are 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 dispose() method.

For information about the interaction between the interface for MATLAB and Java and the JVM, see Interaction Between MATLAB Compiler SDK and JVM.

Name MATLAB Objects for Resource Maintenance

All of the MATLAB objects supported by MATLAB Compiler SDK have 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 object myCA is a wrapper that points to a 5-by-5 mxCellArray object in MATLAB Runtime. myCA can be added to other MATLAB arrays or manipulated in your Java application. When you are finished with myCA, you can clean up the 5-by-5 mxCellArray by using the object’s dispose() method.

The semantics of the API allows you create anonymous MATLAB objects and store them in named MATLAB objects, but you should never do 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 in MATLAB Runtime. When you dispose of data, all of the resources for it are cleaned up. However, the anonymous object created by new 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 Runtime never cleans up the resources it has allocated.

Now consider the following code, where the MATLAB object’s set() 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 dispose() method is called, all of the resources are cleaned up.

Release Resources of MATLAB Objects

Clean up MATLAB objects by using the:

  • Object’s dispose() method

  • Static 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 the MATLAB Runtime mxArray object, it is also deleted.

The following code disposes of a MATLAB object using its dispose() method.

MWCellArray myCA = new MWCellArray(5, 5);
...
myCA.dispose();

The following code disposes of a MATLAB object using the MWArray.disposeArray() method.

MWCellArray myCA = new MWCellArray(5, 5);
...
MWArray.disposeArray(myCA);

Related Topics