Analysis and diagnosis of flex application memory leakage 2

Source: Internet
Author: User

No parameter method call of the object itself. A reference to the "This" keyword is created in the method body, which is not released in time, resulting in Memory leakage.

The following sectionProgramIs the setfocus () of uicomponentSource code:

Listing 3. uicomponent.

  Public  function setfocus ():  void  {  var  SM: isystemmanager =  systemmanager;   If  (SM & (SM. stage |  SM. useswfbridge () {  If  (uicomponentglobals. calllaterdispatchercount =  0  ) {SM. stage. focus  =  This  ; uicomponentglobals. nextfocusobject  =  null   ;}...  

After the setfocus () method is called, The Global Object systemmanager generates a reference to uicomponent through SM. Stage. Focus = This. If no processing is performed later, memory leakage may occur. Here is just an example. The setfocus () method flex of uicomponent has been processed and will not cause memory leakage. You can use it with confidence.

But in the daily programming process, you must pay attention to this very hidden situation.

Implicit reference

The most common case of implicit reference is to add an event listener. A. addeventlistener ("leak", B. leakhandler );

The "listener" attribute of object A creates a reference pointing to the leakhandler method of object B, as shown in,

Figure 1. register an event listener reference diagram

Even if B is set to null, As long as object A is not recycled, B will not be recycled, leading to memory leakage. The weak quote method can be used to avoid Memory leakage, which will be described in later sections.

Introduction to flex builder profiler tools

Adobe provides a profiler tool in flex builder 3 for flex memory diagnosis and performance tuning. This article focuses on profiler's memory diagnostic functions.

Profiler startup

In the flex development view, right-click the main portal file and choose profile as> flex application to start the profiler tool.

As shown in:

Figure 2. Start profiler locally

Profiler parameter configuration

After the profiler is started, a dialog box is displayed to allow you to configure profiler parameters, as shown in:

Figure 3. profiler parameter configuration

The "enable performance profiling" option is used for performance tuning and mainly used to locate the bottleneck of response time. This item is removed during memory debugging.

The "generate object allocation stack traces" option can trace the entire process of object creation. This feature consumes a lot of system resources. In the early stage of debugging, it aims to find the memory leakage object, instead of the creation process, do not select this option.

After the configuration is complete, click resume to continue execution. After a few seconds, the profiler tool starts running, as shown in:

Figure 4. Main Interface for running profiler

Common profiler Functions

The "Profile" window displays the running SWF application. After the selected settings are selected, a series of buttons in the window become available, as shown in:

Figure 5. profiler window

 

Here we will briefly introduce the functions of the buttons related to memory debugging:

1 "Run Garbage Collector" in Figure 5. Click this button to forcibly execute a memory recycle.

Figure 5 2 "Take memory sanpshot": click this button at any time of operation. The system automatically executes a forced memory reclaim operation and then captures a memory snapshot, as the Sub-object of the currently running application, as shown in:

Figure 6. Capture memory snapshots

Double-click the memory snapshot. A tab is displayed in the lower window, showing the objects in the memory snapshot, including the packages, instances, and percentages of the objects, memory usage, and percentage, as shown in:

Figure 7. Objects in the memory Snapshot

3 "find loitering objects" in Figure 5 to find the "Wandering object ". This button needs to select two memory snapshots at the same time (Press and hold the ctrl key to click the memory snapshot) to be useful,

As shown in

Figure 8. Select two memory snapshots

It compares two memory snapshots to find out the objects that exist in the last memory snapshot and do not exist in the previous memory snapshot. In some specific scenarios, this function can quickly find Memory leakage objects.

The profiler tool is used to diagnose Memory leakage. The memory snapshot comparison method is used to diagnose Memory leakage. The most intuitive appearance of the Flex application memory leakage is that when users perform some identical operations, the memory and object instances will continue to increase, even if zookeeper is recycled, the memory will not return to the original level. In the previous chapter, we learned about the main features of profiler, which can capture memory snapshots at any time. As you can imagine, every time you perform the same operation, we capture a memory snapshot and compare several snapshots to find the continuously increasing object instance, you can discover the objects that cause memory leakage, discover vulnerabilities in the program, and finally solve the problem.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.