User`s guide

3 Programming
heap, MWArray objects should be explicitly freed as soon as possible by the
application that creates them.
Using the dispose Method
The best technique for freeing resources for classes created by MATLAB
Builder for Java is to call the
dispose method explic itly. Any Java object,
including an
MWArray object, has a dispose method.
The
MWArray classes also have a finalize method, called a finalizer, that
calls
dispose. Although you can think of the MWArray finalizer as a kind of
safety net f or the cases when you do not call
dispose explicitly, keep in mind
that you cannot determine exactly when JVM calls the finalizer, and the JVM
might not discover memory that should be freed.
Code Fragment: Using dispose
The following example allocates an approx imate 8 MB native array. To
theJVM,thesizeofthewrappedobjectisjustafewbytes(thesizeofan
MWNumericArray instance) and thus not of significant size to trigger the
garbage collector. This example shows why it is good practice to free the
MWArray explicitly.
/* Allocate a huge array */
int[] dims = {1000, 1000};
MWNumericArray a = MWNumericArray.newInstance(dims,
MWClassID.DOUBLE, MWComplexity.REAL);
.
. (use the array)
.
/* Dispose of native resources */
a.dispose();
/* Make it eligible for garbage collection */
a = null;
The statement a.dispose() frees the memory allocated by both the managed
wrapper and the native MATLAB array.
3-26