Notes on flex programming: Performance Optimization and garbage collection

Source: Internet
Author: User

The following content is obtained from a QQ group. I have sorted out and supplemented it, hoping to help you.
Note: The following content is not original to me. Do not repost it without authorization. This article is only used for record and summary. :)
Summary of some knowledge about garbage collection:
1. All references of the deleted object must be deleted to be disposed of as garbage collection by the system.
2. If the child object inside the parent object is referenced by other external objects, the child object will not be deleted, and the child object will not be deleted.
3. If an object references an external object, you must set the reference of this object to null when you are deleted or do not need to use this reference object.
4. The reason why this object cannot be deleted may not be because it has been referenced by itself. It may also be because the child has been referenced by external entities and cannot be deleted by the father.
5. Apart from references that need to be deleted, system components, global tools, and management classes must call the detach method to delete internal objects. Otherwise, memory leakage and performance loss may occur.
6. If the parent object is deleted immediately, it does not mean that the child object will be deleted or immediately deleted. It may be automatically deleted by the system or deleted in the second operation.
7. If the parent object does not clear references to the child object after removing the child object, the child object cannot be deleted, and the parent object cannot be deleted.
8. If a registered event is not removed, the custom forced recovery mechanism will not be affected, but the normal recovery mechanism may be affected, therefore, it is best to delete all registered event listeners.
9. If the parent object is deleted, it does not mean that all other sub-objects are deleted. A state of leakage is found. Code If it is not the same as other States, no leakage will occur. test and analysis should be performed one by one for each module until the entire object can be deleted in any State of the test.
10. When an event is triggered and is no longer used, remove it.
11. If effect can be used, do not use effect.
Example of Memory leakage:
1. Reference leakage: this refers to the reference of the Child object. The external references to this object or sub-object must be set to null.
2. system leakage: The system class is used and the deletion operation is forgotten, such as bindingutils. bindsetter (), changewatcher. after the Watch () function is complete, you need to call changewatcher. the unwatch () function clears references. Otherwise, the objects using this function will not be deleted. Similarly, there are music, video, image, timer, event, binding, and so on.
3. effect leakage: When you apply the effect to the component, you need to stop the effect animation on the object and sub-object when the object is deleted, then, set the target object of effect to null. If you directly set effect to null without stopping the animation, the object cannot be removed normally.
4. SWF leakage: to completely delete a SWF, call its unload () method and set the object to null.
5. Image leakage: Set Source to null after the image object is used.
6. sound and video leakage: if you do not need a music or video, you need to stop the music, delete the object, and set the reference to null.
Memory leakage solution:
1. perform garbage processing in the removed_from_stage event return of the component (remove all external references (both vo and components need to be deleted), delete the listener, and call the clearing method of the system class) remove and then set null to make sure that all objects after being removed or removeall are released in the external reference.
2. use the flex performance optimization tool profile to monitor the project process. You can know which objects have been created in history, which objects have not been deleted, and the number of created objects, memory usage, creation process, and other information.
Conclusion: The key is to clear the file, remember to delete the reference you set, and remember to recycle the system classes you used. If the preceding problem is solved, the user-defined forced recycler may be automatically reclaimed by the system normally.
As we all know, because the garbage collection mechanism of Flash Player is automatic, even if the content above meets the requirements, the memory will still be "too high.
Therefore, I will introduce an unconventional method to keep the garbage collection mechanism of Flash Player under my control. (The following content is not my first one, but I would like to summarize it)
Forced garbage collection: (the famous hack method)
By intentionally making SWF an error during running, and then throw an error, and by catch error, the SWF file continues to run. The garbage collector is forcibly executed once when SWF throws an error to clear invalid data usage in memory and reduce resource consumption.
Below is a code that I found to handle garbage collection using this hack method:

package util
{< br> Import flash.net. localconnection;
Import flash. system. system;
public class memory {
Public Function Memory () {
// to do
}< br> Public static function GC (): void {
try {
New localconnection (). connect ('foo');
New localconnection (). connect ('foo');
}catch (E: *) {}< BR >}< br> Public static function get used (): number {
return syst Em. totalmemory;
}< BR >}< br> there are two ways to use the above Code:
1. At the beginning of the project, create a timer and execute memory every minute. GC ();
2. Find a general-configuration machine and run the Program you want. Then, when the CPU and memory usage are high, record the memory value at that time, and then execute memory where you think it is needed (for example, after bitmap operations and effect effects are completed. GC ();

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.