`

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;
			}
		}
	}
}


分享到:
评论

相关推荐

    Managing NFS and NIS

    在《Managing NFS and NIS》这本书的第一章中,作者深入浅出地介绍了网络的基础知识。这部分内容对于理解后续章节中的NFS(Network File System)和NIS(Network Information Service)至关重要。 #### 1.1 网络...

    memory leakge & initialization & invalid pointer

    Pointers are powerful tools for managing memory, but they can also be dangerous if not handled correctly. An invalid pointer is one that points to an address that is not accessible or has been ...

    Managing Gigabytes

    Managing Gigabytes 很经典的关于大数据处理的书籍,废了很大力气转成pdf 编辑了目录。

    [request_ebook] Managing the Testing Process: Practical Tools and Techniques for Managing Hardware and Software Testing

    Rex Black's Managing the Testing Process, a compendium of real-world advice on managing software testing successfully. It is a veritable hodge-podge of sample test documents and is filled with ...

    Fstream: Managing Flash Sreams in the File System PPT

    - **Flash Translation Layer (FTL)**: This layer is crucial as it manages the translation between logical addresses used by the file system and physical addresses on the flash memory. It helps in ...

    Managing Projects With GNU Make

    In the third edition of the classic Managing Projects with GNU make, readers will learn why this utility continues to hold its top position in project build software, despite many younger competitors...

    Managing for Stakeholders

    1 Managing for Stakeholders, 1 2 Business in the Twenty-first Century, 20 3 The Basic Framework, 47 4 Stakeholders, Purpose, and Values, 74 5 Everyday Strategies for Creating Value for Stakeholders, ...

    Managing Windows with VBScript and WMI 2004

    Managing Windows with VBScript and WMI 2004.chm 共享资源,

    Managing Your Biological Data with Python 无水印pdf

    Managing Your Biological Data with Python 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...

    A+ Guide to Managing and Maintaining Your PC

    A+ Guide to Managing and Maintaining Your PC

    Managing Software Debt(管理软件欠债)

    《Managing Software Debt》一书由Chris Sterling撰写,旨在深入探讨技术债务的概念、原因及其对业务的影响,并提供一系列实用的策略来管理和减少这种债务。 #### 技术债务的概念与类型 技术债务可以分为几种不同...

    Managing and Mining Uncertain Data

    《Managing and Mining Uncertain Data》一书不仅为读者提供了不确定数据管理和挖掘的基础理论框架,还展示了该领域最新的研究成果和技术进展。对于希望深入了解该领域的研究人员、工程师以及学生来说,本书是一本...

    Managing RAID on Linux

    本书《Managing RAID on Linux》由Derek Vadala编写,是一本深入探讨如何在Linux环境下管理和配置RAID系统的专业教材。该书首先介绍了RAID的基本概念和技术背景,然后详细讲解了在Linux系统中实现RAID的各种方法,并...

    Managing Projects with GNU make 3rd

    Managing Projects with GNU make, 3rd Edition provides guidelines on meeting the needs of large, modern projects

    Using and Managing PPP

    ### 使用与管理PPP详解 #### 一、PPP简介 ...以上仅为《Using and Managing PPP》这本书的部分内容概述。该书详细介绍了PPP协议的各个方面,并提供了实用的操作指南和技术细节,是一本值得深入研究的专业书籍。

    Infrastructure as Code Managing Servers in the Cloud epub

    Infrastructure as Code Managing Servers in the Cloud 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Mike Meyers' CompTIA A+ Guide to Managing and Troubleshooting PCs Lab azw3

    Mike Meyers' CompTIA A+ Guide to Managing and Troubleshooting PCs Lab Manual(5th) 英文azw3 第5版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn...

    developing and managing a successfull payment cards business

    developing and managing a successfull payment cards business

Global site tag (gtag.js) - Google Analytics