This is simple test on AS3 for BitmapData memory allocation.
private function memoryTest(): void
{
trace("BitmapData memory test. Create bmps..."); // First breakpoint
var bmps:Array = new Array (1000);
for (var i:int=0; i<1000; i++)
{
bmps[i] = new BitmapData(451, 451, true, 0);
trace(i+". bmp created");
}
trace("All bmps created."); // Second breakpoint
for (i=0; i<1000; i++)
{
bmps[i].dispose();
bmps[i] = null;
}
bmps.splice(0, bmps.length);
bmps = null;
freeMemoryGC();
trace("All bmps deleted.");
trace("Test complete."); // Last breakpoint
}
private function freeMemoryGC(): void
{
// the GC will perform a full mark/sweep on the second call.
try
{
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
}
catch (e:*)
{
}
System.gc();
System.gc();
}
There are 3 breakpoints in this test, that are commented in code. Breakpoints pause program during some time. Breakpoints correspond to the horizontal lines on this memory state chart.
My question is: why not all memory was free after dispose() calls? What wrong in this code and how to clear BitmapData objects properly?
UPDATE 1: I don't think that problem is in garbage collector. dispose() works out of GC and it should release the pixels data. Also this example should allocate 451*451*4*1000 bytes theoretically. But this test application allocates 25% more bytes in System.privateMemory and these 25% is never released by GC and dispose().
UPDATE 2: If I create 13 images 4059x4059 px instead of 100 images 451x451 px, then memory allocates exactly equals to theoretical size and memory is released properly after call GC! I don't know why it happens.
UPDATE3:
Here is my results of interval test, created by Daniel MesSer in him answer: