SoFunction
Updated on 2025-04-08

FLEX memory optimization tips collection

1. All references to the deleted object on the outside must be deleted before they can be processed as garbage collection by the system;
2. The child object inside the parent object is referenced by other external objects, which will cause the child object to not be deleted, and the child object to not be deleted, and the parent object to not be deleted;
3. If an object refers to an external object, when it is deleted or does not need to use this reference object, remember to set the reference of this object to null;
4. The reason why this object cannot be deleted is not necessarily because it has been cited, it is also possible that its child has been cited externally. The child cannot be deleted, so that the father cannot be deleted;
5. In addition to the reference that needs to be deleted, if system components or global tools and management classes provide uninstall methods, they must call to delete internal objects, otherwise it may cause memory leakage and performance losses;
6. If the parent object is deleted immediately does not mean that the child object will be deleted or deleted immediately. It may be deleted when it is automatically deleted by the system or the second removal operation in the later stage;
7. If the parent object removes the child object and does not clear the reference to the child object, the child object cannot be deleted, and the parent object cannot be deleted;
8. If the registered event is not removed, it will not affect the customized forced recycling mechanism, but it may affect the normal recycling mechanism. Therefore, it is best to remember to remove all registered event listeners.
9. The parent object is deleted does not mean that the other child objects have been deleted. If the leaked code in one state is found, it does not mean that other states are not leaked. Each module and state must be tested and analyzed one by one until the entire object can be deleted under any state.


Examples of memory leaks:

1. Quote leakage: References to sub-objects and external references to this object or sub-objects need to be nulled;
2. System class leak: I forgot to delete the system class, such as (), after the () function is completed, you need to call the () function to clear the reference, otherwise the object using this function will not be deleted;
Similar ones include MUSIC, VIDEO, IMAGE, TIMER, EVENT, BINDING, etc.
3. Effect leak: When applying effect to components, when deleting this object, you need to stop the Effect animation on this object and child objects, and then set the Effect target object null; if you do not stop the animation and set the Effect null directly, the object will not be removed normally.
4. SWF leak: To completely delete a SWF, you must call its unload() method and set the object to null;
5. Image leak: When the Image object is used, the source must be nulled; (for test);
6. Sound and video leakage: When there is no need for a music or video, it is necessary to stop the music, delete the object, and null the reference;

Memory leak solution:
1. Do a garbage disposal operation in the component's REMOVED_FROM_STAGE event retrieval (removal of all external references (either VO or component needs to be deleted), delete the listener, and call the system class's clearance method)
Remove first and then null to ensure that all external references of objects that are removed or removedAll are released cleanly;
2. Use Flex's performance optimization tool Profile to monitor the project process, and you can know which objects have been created in history, which objects have not been deleted at present, the number of created, the proportion of memory occupied and the amount of creation, the creation process and other information;


Summary: The key is to do a good job of clearing the references you set yourself. Remember to delete the system classes you have used, and remember to do a good job of recycling and processing. If the above problems are solved well, it may be automatically recycled by the system without a custom forced recycler.