另一个内存泄露 Yet Another Memory Leak on RemoteObjects/Operations

EXCLAMATION It seems that I am really paranoid to fight memory leaks. Indeed. Here is another one.

Problem description

Assume we have the following function and there is no event listeners registered to the remote object. The entities in the query result should be eligible for garbage collection as soon as the the result arrives.

function executeSqlAndForget():int { 
  remoteObject.select("SELECT e FROM Entity e"); 
}

However, the entities will stay in memory even after garbage collection is run.

The cause of the problem

remoteObject.select() results creating or re-using an existing mx.rpc.Operation, and this Operation caches the last result (mx.rpc.AbstractInvoker._result). So unless you execute another call to the same operation on the same remote object this last result will hang there and not available for garbage collection.

The solution

You simply clear the last result using:

remoteObject.getOperation("select").clearResult();

Memory leaks may happen anywhere. Just be sensitive. Otherwise, do not blame Adobe for ridiculous memory usage and unexpected crash.