Ultrashock Forums > Flash > ActionScript
AS3 - Loading images takes up too much memory

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - Loading images takes up too much memory
Old 2009-07-01

I am frustrated! I am working on an application, using Flash CS3 + AIR, that involves some heavy content loading. I need to fetch some 140 gif files each around 400x400 in resolution and 50KB in size and display them in a TileList. The code that I am using is pretty standard stuff, (excerpts from the cellrenderer of my TileList):
Code:
private function loadImage(imageUrl:String):void
{
	var imageLoader:Loader = new Loader();
	var imageReq:URLRequest = new URLRequest(imageUrl);
			
        imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageFetched, false, 0, true);
        imageLoader.addEventListener(IOErrorEvent.IO_ERROR, imageFetchedFailed, false, 0, true);
        imageLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, imageFetchedFailed, false, 0, true);
			
	imageLoader.load(imageReq);
}

private function imageFetched(e:Event = null):void
{

	if (sectionImageHolderSprite == null)
	{
		sectionImageHolderSprite = new Sprite();
		this.addChild(sectionImageHolderSprite);
	}

	bitmap = new Bitmap((e.target.contentLoaderInfo.content as Bitmap).bitmapData);
	bitmap.smoothing = true;
	bitmap.cacheAsBitmap = true;
	bitmap.name = "bitmap";
	
        /* Set the display size appropriately - maintain the aspect ratio */
	var imageAspectRatio:Number = (bitmap.width < bitmap.height) ? bitmap.width/bitmap.height : bitmap.height/bitmap.width ;

	if(bitmap.width >= bitmap.height)
	{
		bitmap.width = 150;
		bitmap.height = bitmap.width * imageAspectRatio;
	}
	else
	{
		bitmap.height = 150;
		bitmap.width = bitmap.height * imageAspectRatio;
	}
	
	bitmap.x = 25;
	bitmap.y = 25;
			
	/* Add it to the display list */
	sectionImageHolderSprite.addChild(bitmap);
}
One would think that the memory required for the images would be around 500KB each (assuming 3 bytes/pixel of raw bitmap data), totaling around 70MB. But when I see the memory in task manager, it takes around 320 MB. And my memory caps if I reload these images three times!

What is going on here? This is unacceptable Firefox/chrome take around 20MB to display an equivalent page. Why does flash require so much memory? Or am I doing something awful here?
postbit arrow 3 comments | 191 views postbit arrow Reply: with Quote   
Registered User
rocking is offline
seperator
Posts: 13
2009-05-13
rocking lives in India
seperator

Ultrashock Member Comments:
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2009-07-01 #2 Old  
Are you loading in your images sequentially or in a loop? If you are loading them in a loop, I heard that could lead to some memory leaks.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-07-01 #3 Old  
It sounds as though you aren't handling the images correctly (disposing of them etc) if system memory is never released by the application. A 400x400 image in Flash is going to need at least 1.20 MB, each pixel is stored as a 32-bit value (8 bytes). You do the math for 140 images.

Why do the images need to be that big and why do you need to load them all at once? If you're not seeing 140 images on-screen at the same time then you don't need to load 140 images, just load them when they are needed and dispose of them when they are no longer needed.
Reply With Quote  
rocking rocking is offline rocking lives in India 2009-07-01 #4 Old  
Loading is not in a loop. I am passing the urls in the dataProvider of the TileList. Maybe flash does it in a loop internally.

The above code is from the custom cell renderer that I have written to show the images. Actually, it does more than rendering images like adding a few buttons in the cell on mouseover etc and hence I can not use ImageCellRenderer. Is there a way to clean up properly once the tilelist is gone? I had a listener on REMOVE_FROM_STAGE to clean up the loaders and remove event listeners. The problem with that is, and I do not know why this happens - every time any button of any cell is clicked, removedFromStage is triggered for the entire tilelist. So the only way I know to cleanup the tilelist is to set it to null

I am showing only 150x150 size of the images. I load them all at once (in a scrollrect) because otherwise the scrolling of the tilelist is not smooth. Without a scrollRect, with every mouse up/down the cells reload and it is extremely bad blotchy user experience.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: