Understanding Memory Leaks in ActionScript (Adobe Flex)

 

Thanks go to Danny of TheGoldHold.com for a bunch of this information!

In ActionScript, there is no method to manually completely remove an object from memory. That task is soley the responsibility of the Flash Player's garbage collector (GC). The GC operates autonomously, and beyond the "Force GC" button in the Flex Builder memory profiler, there is no method to cause the GC to run. Therefore, memory leaks in Flex can be hard to understand and debug.

I have created a MemoryLeakTest project in Flex Builder that show examples that are and are not memory leaks. The project contains three applications, two with memory leaks and one with out. Each sample application adds and removes a single sub-component 100 times per second. This forces the garbage collector to run, and easily shows when a memory leak occurs. The source code contains comments that explain the memory leaks, and how to avoid them, but I'll go over them briefly here as well.

Flash Player Memory Allocation

Memory allocation speed inside the Flash player runtime is limited by the browser. Therefore, instead of making a large number of small requests for memory, flash player makes a small number of requests for large chunks of memory. Memory deallocation is also a slow process, so flash player is reluctant to give up memory; though it will do so as necessary.

Garbage Collector Specifics

Objects are garbage collected only when they have no other objects that contain strong references. Flash Player's runtime keeps a reference table that is updated each time a reference is made between two objects. The Garbage Collector simply uses that table to determine which objects are completely de-referenced.

The Garbage Collector (GC) runs directly before additional memory is requested. This way flash player can gain the memory resources used by garbage objects, and can re-evalute the need to allocate additional memory; possibly saving time. It is important to note that GC does not run immediately when an object is completely de-referenced. So, even though you've completely de-referenced an object it can and probably will stay in memory.

Identifying Memory Leaks

Flex Builder 3 Pro includes a "Profiler" tool that can be used to identify memory leaks. In that tool is a "Force Garbage Collection" button. When your application is being profiled, simply click that button and monitor the "Live Objects" list. If you believe you have complete removed and dereferenced an object, its number of "instances" should have be reduced.

Use the "Cumulative Instances" to determine how many instances of that object were ever created, and the "Instances" column to determine how many of those instances still exist. If after creating and removing an object, then running "Force GC", those numbers are still the same, you probably have a memory leak.

The "Memory Usage" chart is another way to determine whether a memory leak exists; but only for small applications. The red line represents the maximum memory use, and the blue line represents the current memory use. If the blue line and red line never separate, you have a memory leak.


Bad Memory Usage


Good Memory Usage

Memory Leaks by Reference

Because the garbage collector determines which objects to free by their references, it is important for the developer to keep up with what references they have created. A very simple example of this is creating a reference from a parent object to a child object.

In the following example, a childObject is created, and then a second reference is made to that childObject. Because that second reference still exists at the end of this code, that childObject will never be freed; unless the parent object is garbage collected.

    private var childObject:Object = {test: 'test'};

private var secondReference:Object = childObject;

childObject = null;

Memory Leaks by Event Listeners

Incorrect use of EventListners are the number one cause of memory leaks in ActionScript. EventListeners push references of the callback object into an array on the target object. That reference keeps the Garbage Collector from destroying the call back object. There are a few exceptions to this rule. These do not block garbage collection:

  1. Weak References
  2. Self References
  3. References to Child Objects

Weak references are created by setting fifth argument of "addEventListner" to "true". By default, eventListeners use strong references. Note that using a weak referenced eventlistner, and then having no other reference to an object means that object will be garbage collected, so be careful!

An example of a weak reference event listener.

   someObject.addEventListener(MouseClick.CLICK, handlerFunction, false, 0, true);

Self references are simply references by an object to itself. A common example is adding an eventListener to a component, and handling it inside of that component.

An example of a self reference event listener.

    this.addEventListener(MouseClick.CLICK, handlerFunction);

Child references are references to a child object. When a parent object is garbage collected, that destroys the references to the child object. So usually, this means the child object will also be garbage collected (unless outside references to that child object exist). Therefore, it is safe to add eventListeners to an immediate child object and not worry about memory leaks. You should note that the child object will not be freed until the parent object is freed!

An example of a childObject event listener:

     private var childObject:UIComponent = new UIComponent;

addChild(childObject);

childObject.addEventListener(MouseEvent.CLICK, clickHandler);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
基层社会治理解决方案摘要 基层社会治理是国家治理体系和治理能力现代化的重要组成部分。本文总结了当前基层社会治理的政策背景、建设背景、现状、整体规划及具体解决方案,旨在提升社会治安综合治理能力,实现社会和谐稳定。 一、政策背景与建设背景 近年来,国家高度重视基层社会治理,出台了一系列政策文件,如《关于加强社会治安防控体系建设的意见》、《社会治安综合治理基础数据规范》等,为基层社会治理提供了政策指导和标准规范。在此背景下,各地纷纷推进综治中心建设,形成市、区、街道、社区四级综治中心体系,实现统一指挥、调度、派遣、监督等功能。市级综治中心作为龙头,负责整体规划和资源调度;区县综治中心作为主体,负责矛盾纠纷化解、群防群治等工作;街道综治中心作为支撑,负责具体事务的受理、办理和监管;社区综治中心则作为基础,负责信息采集、矛盾排查等日常工作。 二、当前现状与问题分析 当前,基层社会治理虽取得一定成效,但仍存在一些问题。一方面,各综治中心建设水平参差不齐,信息孤岛现象严重,无法实现数据共享和业务协同;另一方面,基层社会治理手段相对单一,主要依赖人力巡查和事后处理,缺乏智能化、信息化手段支持。此外,基层社会治理还存在公众参与不足、群防群治机制不健全等问题,影响了社会治理效能的发挥。 三、整体规划与目标设定 针对上述问题,本文提出了基层社会治理的整体规划与目标设定。首先,明确建设思路,即坚持党政领导、整合社会资源、实现互联互通和资源共享;其次,设定建设目标,包括构建综治大脑、实现智能应用、打造基础平台等;最后,制定业务概图,明确各级综治中心的职责和任务,以及物联感知平台、智慧治理等创新应用的建设方向。 四、具体解决方案与实施路径 为实现上述目标,本文提出了具体解决方案与实施路径。一是建设基层治理信息平台,采用微服务模式建设社会信息采集平台、社会综合治理基础平台等核心业务层;二是构建大数据应用中心,整合各类数据资源,实现数据清洗、分类占比、任务调度监控等功能;三是推进信息采集平台建设,实现一键操作、极简极易的信息采集方式;四是完善社会综合治理信息平台功能,包括实有人口、实有房屋、矛盾排查处理等模块;五是建立大联动业务协同系统,实现跨部门、跨领域的业务协同和应急指挥。 五、总结与展望 综上所述,基层社会治理解决方案以提升社会治安综合治理能力为核心,通过政策引导、技术支撑、机制创新等手段,推动基层社会治理向智能化、信息化、协同化方向发展。未来,随着技术的不断进步和政策的持续完善,基层社会治理将更加高效、精准、便捷,为人民群众提供更加安全、和谐、幸福的生活环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值