Main Content

Manage MATLAB Resources

Why MATLAB Resources Need to be Managed

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 the MATLAB Runtime. Resource created by the MATLAB Runtime are not visible to the JVM™. The JVM’s garbage collector cannot effectively manage resources that it cannot see.

All of the MATLAB Compiler SDK Java classes have hooks that free the MATLAB resources when the JVM garbage collects the wrapper objects. However, the JVM’s garbage collection is unreliable because the JVM only sees the small wrapper object. The garbage collector can decide that it is not worth wasting CPU cycles to actually delete the wrapper object. Until the Java wrapper object is deleted, the resources allocated in the 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.

Creating MATLAB Objects

All of the MATLAB objects supported by MATLAB Compiler SDK have standard Java constructors as described in the com.mathworks.toolbox.javabuilder Javadoc.

When creating MATLAB objects, always assign them names. To create a 5x5 cell array:

MWCellArray myCA = new MWCellArray(5, 5);

The Java object myCA is a wrapper that points to a 5x5 mxCellArray object in the 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 5x5 mxCellArray 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.

The following code 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 the 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. However, because the Java wrapper consumes a tiny amount of space, the garbage collector is likely to leave it around. Because the JVM never cleans up the wrapper object, the MATLAB Runtime never cleans up the resources it has allocated.

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.

Disposing of MATLAB Objects

There are two ways of cleaning up MATLAB objects:

  • the object’s dispose() method

  • the 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);