This technical document contains information regarding Aion-side object memory cleanup in an Aion Java Interface Layer application.
Release: 9.1
When an Aion-exported Java class proxy object is created in a Java application, object memory is allocated on the Aion side. You can explicitly call the delete() method on the proxy object to de-allocate the object's Aion side memory. Alternately, you can let the AionSession.terminate() method handle the cleanup of the memory allocated on the Aion side for all the objects in the session before it terminates the session. As a fallback scenario, the proxy object delete() method will be called, thru the finalize() method, by the garbage collector when it determines that there are no more references to the object.
The following code sample shows how to let AionSession.terminate() handle the cleanup on the Aion side without calling delete():
// Create an AionSession
AionSession myAionsession = AionSession.getInstance();
// Keep an array of object references
MyObj obj[10];
// Create "MyObj" objects in this session, do something with the objects
for (int i=0; i< 10; i++) {
obj[i] = new MyObj(myAionsession);
// Do something with this object
}
// Terminate the session
// Among other things, this will clean up the memory allocated on the Aion
// side for the above 10 objects.
myAionsession.terminate();
You can also clean up the Aion side memory allocated for each proxy object by explicitly calling delete() as shown below. Because the Aion side memory for the proxy object is already cleaned up via delete(), AionSession.terminate() need not have to do any Aion side object cleanup.
The only difference between the following code snippet and the above is a call to delete() on the proxy object at the end of the "for" loop:
// Create an AionSession
AionSession myAionsession = AionSession.getInstance();
// Keep an array of object references
MyObj obj[10];
// Create "MyObj" objects in this session, do something, call delete()
for (int i=0; i< 10; i++) {
obj[i] = new MyObj(myAionsession);
// Do something with this object
// Clean up this object and delete the corresponding Aion side memory
obj[i].delete();
}
// Terminate the session:
// This does not have to clean up the Aion side object memory because it is already
// cleaned up by calling delete()
myAionsession.terminate();
In general, the approach of using AionSession.terminate() is recommended because the memory is cleaned up for all the session objects together. If a session has a large number of proxy objects, calling delete() separately on each proxy object can be more expensive compared to handling the cleanup thru the AionSession.terminate(). Finally, the call to proxy object delete() during garbage collection is provided only as a fallback scenario and should be used sparingly.