A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [AS3] Memeory Usage... need a pro tip/advice

  1. #1
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379

    [AS3] Memeory Usage... need a pro tip/advice

    I'm writing an application (the one in my latest post in my blog in sig), and here's the scoop: I'm loading in roughly 1000 case images drawn to a bitmapdata and attached to a sprite as a bitmap, while small, range from 15-17k. That part I'm not worried about. Now I'm also duplicating them and creating a mirror effect to place directly below them. Again, I'm not worried about the memory; those 2000 together still only add about 10MB to my app (which is standalone in projector).

    The problem comes when I try to fill the area where the cases appear. After talking with the client, I need to display 3 rows on the screen in a 1920x1080 resolution (will be running on this living room widescreen tv). I'm using a scrollrect and all those goodies to keep all nice and neat. What happens is that when I try to use a bitmapFill to show the "shelf" that each case is sitting on, the RAM footprint skyrockets. Check out this image to get an idea for what I'm working with. I tried to fill the background of the body window with a few more shelf images than I thought he'd ever use, but that wound up being a 1905x52800 bitmapfill (roughly 113MB w/ my math). You can also see in that image that I'm pushing 600 MEGABYTES OF RAM without another flash application running at all. (The actual count when I close the projector is just over 700MB in my Task Manager and I'm not even loading all 1000, just 150). My first assumption was an obvious memory leak, combined with the huge bitmapfill + 300 or so bitmaps/bitmapdats/sprites, etc.

    However, I was just testing the app from the Flash CS4 IDE when I noticed something, at the EXACT same state as in Projector, my RAM footprint is only 190-240MB. Does anyone know anything about Projector and why it eats up so much extra RAM? Also, if anyone can think of any tricks I can fake out the client with to display the shelves behind the images correctly, I'd appreciate the advice.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Is the background image is separate? If its only purpose is to be on background you could make the actual bitmap much smaller and scale it up on screen, this should reduce memory usage.

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    It's not. For proper display, it's a bitmap fill of a 1x290 bitmap with transparency.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  4. #4
    I would make the background a Sprite container with a whole bunch of Bitmap tiles inside that reference only one BitmapData object.

    I made this code quickly, and is untested. But basically it takes the original pattern specified by you previously (1 * 290) as a BitmapData object and returns one Sprite Background. You can then add the background to the display list.

    Code:
    public function getBackground(pattern:BitmapData):Sprite{
    	var background:Sprite = new Sprite();	
    
    	//320 pix by 290
    	var tileShape:Shape = new Shape();
    	tileShape.graphics.beginBitmapFill(pattern);
    	tileShape.graphics.drawRect(320, 290);
    	tileShape.graphics.endFill();
    	
    	//I think if you store your data here it will only use memory needed for 1 tiles
    	//because everything is rendered at runtime using that one tile data.
    	var tData:BitmapData = new BitmapData(320, 290, true, 0);
    	tData.draw(tileShape);
    
    	
    	var tileBitmap:Bitmap;
    	for(var x:int = 0 ; x < 6 ; x++){
    		for(var y:int = 0 ; y < int( 52800 / 290 ) ; y++){
    			tileBitmap = new Bitmap(tData);
    			tileBitmap.x = x * 320;
    			tileBitmap.y = y * 290;
    			background.addChild(tileBitmap);
    		}
    	}
    	return background;
    }
    I believe you can save alot of memory by creating only one tile, or bitmapData, and then using it to render onto the screen all the tiles.

    Hopefully the above code can be a helper for you, or perhaps if I wrote everything correct, a solution.

  5. #5
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    I'll have to try it, but I have a very small margin for balance. As I said, at a minimum--not including GUI sprites and stuff like a menubar, scrollbar, containers, I have 2 times the number of movies in the database in sprites. I just recently switched it over to be, instead of 1 container clip, one inside for the image and a third sprite for the mirror for EACH image, that I just drew the contents of the container and removed the two inside. That cut down on file size a little. My point is that I can't tile anything that many times which is originally why I tried a single bitmap fill.

    My other untested theory was to bitmap fill only 3 rows, and move it when the scroll bar moves and keep it on screen. If the y of the background is less than -290, I'd just add 290 to it; same if it goes above 0.

    I appreciate the advice though.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center