Notes on Memory Management with Java OpenMaple
In Java OpenMaple, a Maple expression is represented by an Algebraic object. As long as this object is alive a reference to the corresponding Maple expression is kept. This reference keeps the Maple expression from being collected by the Maple garbage collector. Once the Algebraic object is freed in Java, the Maple expression may also be freed by Maple.
In general, this system works well and allows the user to ignore the details of memory management. Unfortunately, there are some situations where this system leads to poor memory usage. In these situations the user must take more responsibility for some memory issues.
This system fails when Java is creating many Algebraic objects without invoking the Java garbage collector. An example of this is a tight loop that creates an Algebraic for each iteration of the loop:
for ( i = 0; i < 100000; i++ )
kernel.evaluate( "Array( 1..100000, fill="+i+");" );
In this situation Maple is using a large amount of memory, but Java is not. Since Java is not using much memory, it does not invoke its garbage collector frequently. This means that the Algebraic objects returned by evaluate are not freed often. Therefore, the Maple rtables referred to by the Algebraic objects are not collected. Since these objects use a large amount of memory, Maple is forced to allocate a large amount of memory from the system.
Calling dispose breaks the link between the Algebraic and the corresponding Maple expression. By doing so Maple is free to collect the expression. The above code should be rewritten as follows:
a = kernel.evaluate( "Array( 1..100000, fill="+i+");" );
In this case Maple is free to collect the memory used by an rtable any time after dispose has been called. Once the dispose member function has been called on an Algebraic no other member functions may be called except isDisposed.
Download Help Document