`

Managing Memory[转自David Pett的博客]

阅读更多
I have been doing a lot of work in try­ing to min­i­mize mem­o­ry and file size in my flash/ac­tion­script pro­jects, be­cause of the struc­ture of ac­tion­script 3 most of this is a man­u­al pro­cess. One of the first things you can do is when adding an event lis­ten­er, use the op­tion­al pa­ram­e­ters:
_btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);

The last op­tion­al pa­ram­e­ter in the ad­dE­ventLis­ten­er func­tion is useWeakRef­er­ence, which by de­fault is set to false, ac­cord­ing to the Ac­tion­Script 3.0 Doc­u­men­ta­tion, this pa­ram­e­ter “De­ter­mines whether the ref­er­ence to the lis­ten­er is strong or weak. A strong ref­er­ence (the de­fault) pre­vents your lis­ten­er from being garbage-​col­lect­ed. A weak ref­er­ence does not.”

An­oth­er stan­dard that I have ime­ment­ed is to use the RE­MOVED_FROM_STAGE event in every class that I write. In this class I re­move any dis­play ob­jects that I have added and re­move all event lis­ten­ers that those dis­play ob­jects have, be­cause just re­mov­ing an ob­ject does not do this and hose event lis­ten­ers will still be there taken up mem­o­ry and can hin­der per­for­mance.

我一直在试图减少内存和文件大小在我的flash/ActionScript项目,由于ActionScript 3的这方面最结构,是一个手动过程。第一件事你可以做一个是当添加一个事件监听器,使用可选的参数:
  _btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);

最后在addEventListener函数的可选参数useWeakReference,默认设置为false,根据ActionScript 3.0文档,此参数:“确定是否对侦听参考的强弱。强引用(默认值)可以防止垃圾回收收取。弱引用没有。”

另一个我imemented标准是在每个类使用我写的REMOVED_FROM_STAGE事件。在这个类中删除任何显示对象,我增加和删除所有事件监听器,这些显示对象的,因为就删除一个对象不这样做,事件事件监听器依然占用内存,可阻碍性能对象。

package
{
	import flash.display.*;
	import flash.events.*;

	public class MyClass extends Sprite
	{
		private var _btn:Sprite;
//-----------------------------------
//	CONSTRUCTOR
//-----------------------------------
		public function MyClass():void
		{
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
//-----------------------------------
//	INIT
//-----------------------------------
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose, false, 0, true);

			_btn = new Sprite();
			_btn.addEventListener(MouseEvent.MOUSE_OVER, btnOver, false, 0, true);
			_btn.addEventListener(MouseEvent.MOUSE_OUT, btnOut, false, 0, true);
			_btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
			_btn.buttonMode = true;
			addChild(_btn);
		}
//-----------------------------------
//	DISPOSE
//-----------------------------------
		private function dispose(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);

			if(_btn)
			{
				_btn.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
				_btn.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
				_btn.removeEventListener(MouseEvent.CLICK, btnClick);
				try
				{
					removeChild(_btn);
				}
				catch(e:Error){trace("error: MyClass: dispose: removeChild(_btn): " + e)}
				_btn = null;
			}
		}
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics