Hi,
I have a memory leak happening in the onUpdate() function of my views. Every time it is called, some memory is being allocated and not released, which after a few updates leads to an Out of Memory error.
I have reduced my code down to the following bits which seem to cause the leak, but I cannot figure out why it happens.
First, my very simple onUpdate() that I use for testing. It instantiates an container object (DummyContainer) and then adds an element to it.
function onUpdate(dc as Dc) as Void {
System.println( "onUpdate: s " + System.getSystemStats().usedMemory );
var obj = new DummyContainer( {} );
obj.addElement( {} );
System.println( "onUpdate: e " + System.getSystemStats().usedMemory );
}
Here the code for DummyContainer and two more classes used in its implementation:
class DummyContainer extends DummyBase {
protected var _elements as Array;
function initialize( options as Dictionary<Symbol,Object> ) {
DummyBase.initialize( options );
_elements = new Array[0];
}
function addElement( options as Dictionary<Symbol,Object> ) {
options[:parent] = self;
_elements.add( new DummySub( options ) );
// new DummySub( options ); // with this line instead of the one above, there is no leak
}
}
class DummySub extends DummyBase {
function initialize( options as Dictionary<Symbol,Object> ) {
DummyBase.initialize( options );
}
}
class DummyBase {
private var _options as Dictionary<Symbol,Object>;
// Constructor
function initialize( options as Dictionary<Symbol,Object> ) {
_options = options;
// _options = { :test => "test" }; // with this line instead of the one above, there is no leak
}
}
As you can see the addElement function instantiates another object (DummySub) and stores it in the _elements array. Both that object and the container have the same base class (DummyBase), which stores the options dictionary in the _options member.
With the code as it is shown, the memory leak occurs. However if I either DO NOT add the DummySub to _elements OR DO NOT store the dictionary in _options, no memory leak occurs. See the alternative, commented out lines.
I do not understand why what I am doing here leads to a memory leak and what I could do differently. Any advice is highly appreciated!
So far I tested this only on the simulator - could it be a bug in the simulator and even not occur on real devices?
Here the debug output with the memory leak:
onUpdate: s 19840 onUpdate: e 20448 onUpdate: s 20448 onUpdate: e 21056 onUpdate: s 20936 onUpdate: e 21544 onUpdate: s 21544 onUpdate: e 22152 onUpdate: s 22152 onUpdate: e 22760 onUpdate: s 22760 onUpdate: e 23368
Compared to that, if I replace either of the above-mentioned lines with the commented-out version, after the first two calls, the memory stays stable:
onUpdate: s 19840 onUpdate: e 20448 onUpdate: s 19840 onUpdate: e 20448 onUpdate: s 19720 onUpdate: e 20328 onUpdate: s 19720 onUpdate: e 20328 onUpdate: s 19720 onUpdate: e 20328 onUpdate: s 19720 onUpdate: e 20328