Large memory build-up when modifing object properties using string keys => object["someKey"]=
Assigning a rational number to an object internal value using a string key selector generates garbage. Ex:
var rational: Number = 0.1;
someObject["someValue"] = rational;
My current use case:
I was developing a small custom animation engine ( basically a tweener ). When I tested it with about 400 tweening objects at the same time, I noticed in Scout a large memory build-up ~1-2 MB/s ( NOT A memory leak as it was getting GC'ed). Weird thing was that Scout (or any other profiler for that matter) did not show what objects were created/responsible for the build-up. I spent a day tracking this issue down to the very line that causes this. It seems that setting an object property using a string key like this:
"object["someKey"] = someValue" generates some internal Flash garbage ( not caught by profiling). Doing this repeatedly on every frame becomes noticeable. Every generic object tweening engine I tested suffers from this.
What baffled me was that if the value( "someValue" ) assigned to the objects property was an "int" type
(ex: "object["someKey"] = 5") did not generate garbage(total memory flat line in scout). If it was a rational number then it generated MB/s triggering GC every ~5 secs.
Not sure if it is a flash bug or a "normal" thing (because I think there is some sort of internal lookup for the property inside the object) but I am curious for an explanation. I am pasteing here a minimal code example to reproduce this issue.
Minimal sample code to reproduce this issue:
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="800", height="480", frameRate="60" , backgroundColor="0x000000")]
public class Main extends Sprite
{
private var objectsArray:Vector.<ValueObject> = new Vector.<ValueObject>;
private var rational: Number = 0.1;
private var integer: int = 1;
private var maxObjectsCount: int = 1000;
public function Main()
{
stage.addEventListener( Event.ENTER_FRAME, update, false,0, true );
//generate objects
for (var i:int=0; i<maxObjectsCount; i++)
{
var object: ValueObject = new ValueObject();
objectsArray.push(object);
}
}
private function update(event: Event ):void
{
var i:int;
var len:int = objectsArray.length;
for(i=0; i<len;i++)
{
var object:ValueObject = objectsArray;
//this generates garbage
object["value"] = rational;
//this doesn't generate garbage
//object["value"] = integer;
//this doesn't generate garbage
//object.value = rational;
}
}
}
}
internal class ValueObject
{
public var value: Number = 0;
}
