I was doing some experimenting to see whether having multiple references to a
string took up much space, and it turns out that Flash doesn’t actually deep-copy
strings when you copy them around. Instead it copies by reference until a change
is made, which then forces a deep copy. This is similar to the way PHP works,
passing strings by value but not actually creating a new object until the value
is changed. Below is the code I used to determine this:
importflash.system.System;importflash.utils.ByteArray;function clone(source:Object):*{varcopier:ByteArray=newByteArray();copier.writeObject(source);copier.position=0;return(copier.readObject());}varstr:String="This is a test of string copying";varstoreA:Array=[];varstoreB:Array=[];varbefore:int;// Test Abefore=System.totalMemory;for(vari:int=0;i<10000;i++){storeA.push(clone(str)asString);}trace(System.totalMemory-before);// Total Mem Increase: 3280896// Test Bbefore=System.totalMemory;for(varj:int=0;j<10000;j++){storeB.push(str);}trace(System.totalMemory-before);// Total Mem Increase: 49152// Can you change str without affecting storeB?str="New value";trace(storeB[0]);// 'This is a test of string copying'// Can you change storeB[0] without affecting storeB[1]?storeB[0]+='a';trace(storeB[0]+" vs "+storeB[1]);// 'This is a test of string copyinga vs This is a test of string copying'