• Home
  • Contact
  • Careers
  • Socialize With Us:
  • Send us Mail
  • Follow us on Twitter
  • Join our Facebook Group
  • /
  • Linkedin
  • /
  • Vimeo Videos
  • /
  • Youtube
  • /
  • RSS Feed
  • Search Site

  • About UsAwards & Bio
  • PortfolioOur Latest Work
  • TrainingTips & Tutorials
  • BlogGreat Articles

You are here: Home / Tag: Optimization

Tag Archive for: Optimization

Fixing Memory Leaks with Adobe Flash Builder

Category: Quick Tips     |     Tags: AIR, AS3, Debugging, Flash, Flex, Optimization

On a recent project, we had a performance problem. The AIR project’s requirements include the heavy adding/removing of UI elements and over time we could easily see the performance slowed. We knew there was a something wrong. It was a memory leak. A memory leak starts when an object is stored in memory but cannot be accessed by the running code. When the undesired object cannot be freed from memory, and is not usable, it is a leak. Over time the application may leak (or re-leak the same issue) more and more. Eventually the application may show signs of the leak or even become unusable.

User Experience Symptoms Of  A Memory Leak

  • During regular use the application becomes more and more sluggish/slow. If there is nothing ‘new happening’ onscreen and no heavy ‘rendering’ it is more obvious to notice the Framerate-per-second (FPS) lower. You can use a small debug window (such as Hi-Res-Stats formerly MrDoobs Stats) to show the current FPS and estimated ram usage to help you notice this. If you see the FPS run at 30 for example during the first minute of use and 20 after 5 minutes, there may be a memory leak.
  • The application quits suddenly. This could be for many reasons, but it may be that the application runs out of memory.
  • Flash throws the memory-specific error ‘flash.errors.MemoryError’
Even if you see one of these symptoms it may not be obvious that there is a problem or that the problem is a memory issue. Or perhaps your development machine is high powered with ample RAM, yet your target user’s machines are slow. You don’t see the issue, but your users will. So how can we diagnose the issue. Luckily, the Flash Builder IDE has a ‘Profiler’. This program runs alongside your application and serves several key roles. You can monitor your applications USAGE of memory over time, and see a live list of all OBJECTS in memory (including references to those objects).

Profiling To Find Evidence Of A Memory Leak

  • Run your application with the Flash Builder Profiler. Run -> Profile As… -> etc…
  • Watch the ‘Memory Usage’ Panel
  • Look at the curves of Peak Memory (Red) and Current Memory (Blue). The analysis is totally application dependent. In your particular application, if you expect memory not to grow, but you see it grow, that is a problem. If you expect the memory to drop (UI removed from stage, arrays and vectors emptied, etc…) and you don’t see it drop, that is a problem. Herein lies the art of memory profiling. Consider to add a button to ‘Reset Application’, then click it and see that indeed the Current Memory drops to zero (0).
  • Watch the ‘Live Objects’ panel. Compare the 1. ‘Cumulative Instances’ and 2. ‘Instances’. For each object. #1 shows the total objects every created since the application started and #2 shows only those currently in memory. If these numbers are the same, and should not be, that is a problem. Perhaps you feel you have deleted a sprite from the stage or deleted another object from memory yet it still exists.


Tools And Tips For Finding And Fixing Memory Leaks (Must Read!)

  • Memory Tracker – http://divillysausages.com/blog/tracking_memory_leaks_in_as3.
  • Solving Memory Leaks (FANTASTIC!) – http://www.tikalk.com/flex/solving-memory-leaks-using-flash-builder-4-profiler
If you want to play with a simple example you can download the example below (See ‘Member Resources’) below.

Example 1: Without Memory Leak

package
{
	import flash.display.Sprite;
	import flash.events.Event;

	//USE A LOW FRAMERATE, SO WE CAN STUDY CLOSELY
	[SWF(frameRate="1")]
	public class MemoryLeakDemo extends Sprite
	{

		private var listOfDots_vector:Vector.<CustomDot>;
		public function MemoryLeakDemo()
		{
			//REPEAT SOME CODE EVERY SECOND
			addEventListener(Event.ENTER_FRAME, _onEnterFrame);

			//CREATE A LIST
			listOfDots_vector = new Vector.<CustomDot>();
		}

		protected function _onEnterFrame(event:Event):void
		{
			//EVERY FRAME WE...

			//1. CREATE A NEW 'DOT' (A Red Circle Sprite)
			//MEMORY NOTE: 	'var' is a temporary variable.
			//So CustomDot has 0 (permanent) references
			var customDot : CustomDot = new CustomDot();

			//2. ADD TO THE STAGE
			//MEMORY NOTE: 	So CustomDot has 1 (permanent) reference; 'this'
			addChild(customDot);

			//3. REMOVE TO THE STAGE
			//MEMORY NOTE: 	So CustomDot has 0 reference
			removeChild(customDot);

			//So...
			//THERE IS NO LEAK
			//The GC will *mark* the 'customDot' as having 0 references and
			//The GC will *sweep* it away from memory.

		}

	}
}

Example 2: With Memory Leak

package
{
	import flash.display.Sprite;
	import flash.events.Event;

	//USE A LOW FRAMERATE, SO WE CAN STUDY CLOSELY
	[SWF(frameRate="1")]
	public class MemoryLeakDemo extends Sprite
	{

		private var listOfDots_vector:Vector.<CustomDot>;
		public function MemoryLeakDemo()
		{
			//REPEAT SOME CODE EVERY SECOND
			addEventListener(Event.ENTER_FRAME, _onEnterFrame);

			//CREATE A LIST
			listOfDots_vector = new Vector.<CustomDot>();
		}

		protected function _onEnterFrame(event:Event):void
		{
			//EVERY FRAME WE...

			//1. CREATE A NEW 'DOT' (A Red Circle Sprite)
			//MEMORY NOTE: 	'var' is a temporary variable.
			//   So CustomDot has 0 (permanent) references
			var customDot : CustomDot = new CustomDot();

			//2. ADD TO THE STAGE
			//MEMORY NOTE: 	So CustomDot has 1 (permanent) reference; 'this'
			addChild(customDot);

			//2B. CREATE ANOTHER REFERENCE TO THE DOT
			listOfDots_vector.push(customDot);

			//3. REMOVE TO THE STAGE
			//MEMORY NOTE: 	So CustomDot has 0 reference
			removeChild(customDot);

			//So...
			//THERE *IS* A LEAK
			//While we are correctly calling removeChild
			//There is a reference left in '2B' above.

		}

	}
}

Next Steps

  • Download the code and check it out! (See Member Resources)
  • Comment below with your thoughts.
  • NOTE: Flash Builder 4.7 (BETA2) has new Profiler called “Monocle“. It looks very exciting, yet this article focuses on the currently available (non-BETA) profiler.

Member Resources

[private_Free member]Enjoy this members-only content!

  • Download the source-code to the MemoryLeakDemo

[/private_Free member]

Support Our Sponsors

Category

  • Industry News
  • Standards & Best Practices
  • Full Tutorials
  • RMC News
  • Events

Tag

3D AIR API AS3 AS3.5 AssetStore Augmented Reality Business C# Charity Debugging Design Patterns DevDiary ECS Architecture Experimental Flash Flex Game Design Game Design Prototypes Games GUI HTML5 Java Loom Mobile MVCS Architecture Optimization Project Planning PushButtonEngine ReactiveExtensions Review Robotlegs Smash Testing Unity3D UnityApplicantTest WordPress WordPress Plugin

Brazilean Developers

  • Abendita.com
  • dclick.com.br
  • dm9.com.br
  • Fellyph Cintra
  • IgorCosta.org
  • MonadaSolucoes.com.br
  • PossibleWorldwide.com.br
  • Unit9.com

Developers

  • Adobe Blogs
  • Ben Forta
  • Colin Moock
  • Enrique Duvos
  • Flash Mobile Blog
  • Jess Freeman
  • Kevin Hoyt
  • Lee Brimelow
  • Paul Trani
  • Quasimondo
  • Renaun Erickson
  • Ryan Stewart

Free Assets

  • Free Sounds
  • HasGrafics

HTML5 Games

  • Closure JS Library
  • Eloquent JS Manual
  • Game Framework – CraftyJS
  • Game Framework – EaselJS

Italian Developers

  • alchimedia.com
  • corlan.org/
  • creativesource.it
  • dimix.it
  • fabiobiondi.com
  • gnstudio.com
  • Interpreting-tech.com/bemobile/
  • leonardorisuleo.info
  • lucamascaro.info
  • mart3.org
  • mxml.it
  • nxn.it
  • pirosoft.it
  • Preload.it
  • sonnati.wordpress.com/
  • webgriffe.com

Products

  • Adobe.com
  • Amazon Kindle E-Reader
  • ElectroServer
  • F*CSS
  • Flash Development Toolkit (FDT)
  • O'Reilly PureMVC Book
  • Samsung Galaxy Tablet
  • Unity3D

RMC

  • RMC Consulting

Spanish Developers

  • Flash Adictos
  • HTML Cinqo
  • Tutoriales Flash

Tutorial

  • Active Tuts
  • AS3-to-Unity3D Training Videos
  • Doing 2D in Unity3D
  • Learning C#
  • Unity3D Tutorials

Unity3D Games

  • AS3-to-Unity3D Training Videos
  • Doing 2D in Unity3D
  • Learning C#
  • Matt Eley's Blog
  • Unity3D
  • Unity3D Tools
  • Unity3D Tutorials

I Am Great!

   

Latest Portfolio

  • Coins And PlatformsMarch 19, 2014, 6:04 am
  • Paddle SoccerMarch 2, 2014, 9:13 pm
  • Spider StrikeFebruary 21, 2014, 4:19 am
  • Custom Game System APIJuly 8, 2013, 8:05 am

Latest News

  • RMC Primer: Everything Virtual Reality (VR)September 3, 2016, 10:29 am
  • Unity3D Architectures: EntitasJuly 29, 2016, 11:15 pm
  • RMC Primer: Get A Job In Game DevelopmentAugust 19, 2015, 10:18 am
  • Unity UI: Overview – Part 1 of 3December 10, 2014, 9:55 am

Latest Tweets (@srivello)

  • Stunning graphic design on the #MacPro page. https://t.co/NjUQMVvH9b
  • Apple's #IOS7 killer features are Control Center, AirDrop, Multitasking. https://t.co/G3BIx1Vlwb
  • Nothin in #OSX #Mavericks sounds interesting except BOTH Tags and Tabs. Psyched. https://t.co/DKLOxm2EUb
  • Working on a #Unity3D demo - a browser of architectural models. Nice!

© Copyright 2006 - 2019 - Rivello Multimedia Consulting - Unity Game & Tools Development