Flex memory leaks
FLEX memory optimization techniques collection type: reproduced
FLEX memory optimization principles released
1. Is deleted all references to objects in the external must be removed as a clean garbage collection system can be disposed of;
2. Parent child objects within other objects by an external reference, and will lead to the child object is not deleted, the child object will not be removed will result in the parent will not be deleted;
3. If an object reference to an external object, as they have been deleted or does not need to use this reference object, we must remember that a reference to this object is set to null;
4. The object can not remove the causes are not necessarily that they have been cited, there may be an external reference to their own children, and children can not afford to lead delete delete stubborn father;
5. In addition to references to delete, the system components or whole instruments, management provides the uninstall method if there must be invoked to delete internal objects, or they may result in loss of memory leaks and performance;
6. The parent object is deleted immediately does not mean that the child object will be immediately removed or deleted, the system may later be automatically deleted or second removal operation is deleted;
7. If the parent remove the child object does not have any clear reference to the object pairs, as the child object can not be deleted, the parent object and can not be deleted;
8. Registration of events if not removed from the definition of force does not affect the recovery mechanism, but may affect the normal recovery mechanisms, so it is best to do an event listener registration should remember to remove cleaner.
9. The parent object is deleted does not mean the rest of sub-objects are removed, the disclosure of code to find a state does not mean that other states would not leak, and to the status of each module were tested one by one until the test can be removed under any until the entire object.
Memory leak example:
1. Reference leak: pair object reference, an external object or child object to this reference have to set null;
2. System classes leaks: Use a system class and forget to do the delete operation, and as BindingUtils.bindSetter (), ChangeWatcher.watch () function when finished you need to call ChangeWatcher.unwatch () function to clear the reference, or use this function the object will not be deleted;
There are similar MUSIC, VIDEO, IMAGE, TIMER, EVENT, BINDING so.
3. Effects disclosure: When the Application Effect on components of the time, when the object when the deleted object and child objects to the Effect on the animation stops away, Effect of target objects and then set null; If you do not stop directly out animation Effect null set will not function properly remove the object.
4. SWF disclosure: To completely remove a SWF to call it the unload () method and the target set null;
5. Pictures leak: When Image object source after use should be set null; (for testing);
6. Voice, video leak: When do not need a music or video is the need to stop the music, delete the object, reference set null;
Memory leak solution:
1. REMOVED_FROM_STAGE event back in the components to do away waste operations (removal of all external references (whether VO or components need to remove), remove the listener, calling the removal method of system classes)
First remove and then set null, remove or removeAll ensure that the object after the release of all external references to a clean;
2. Use Flex Profile to the performance of optimization tools to monitor progress on the project, know history, which objects to create Guo, there are objects which have not been removed, to create the number and proportion of the memory usage, and other information to create Guo Cheng;
Summary: The key is to make clear the work, their own set of reference they have to remember to delete their own system of categories used to remember the good recycling work. Above do not solve the problem well, it would force the collector custom may also be automatically recovered by the system out of normal.
Performance for Flex I think a lot of people who really intend to use it the most concern, is also used for so long I understand the issue more deeply,
The current performance I have ever done is a big problem, where I summarized some of the technical performance of the method.
1. To avoid deeply nested containers (because of its sub-containers for each container will be calculated and adjusted, generally recommended HBox, VBox is best not more than three layers, but this is difficult to avoid the complex system)
2. For containers and space, location and size of the absolute coordinate and maximize the use of hard-coded. (Less Flash Player nested depth calculation)
3. Reasonable and appropriate use of Grid container, ordinary containers can be used as for the HBox, VBox to achieve as much as possible not to use Grid (Grid nesting levels deep)
4. To avoid duplication, redundant nested containers, such as:
1) Panel, Application to add a layer of VBox, Panel, Application originally inherited from VBox, and add unnecessary VBox can lower your system's performance.
2) For custom containers to reduce unnecessary duplication of the parent container, such as:
<mx:VBox backgroundColor=" #FFCCCC" borderStyle=" solid">
<myComponent xmlns=" *" />
</ Mx: VBox>
You can use the following wording, which itself is likely to myComponents VBox
<myComponent xmlns=" *" backgroundColor=" #FFCCCC" borderStyle="solid" />
5. Latency navigation class to instantiate containers (Deferred Instantiation Navigator Containers)
If ViewStack, Accordion, TabNavigator so, in itself provides a delay function is instantiated, you only need to modify their creationPolicy = 'auto' can be, it will only instantiated the first to show the child controls, and the rest will be needed when instantiated.
6. For the flex effect (Effects) to be as smooth as possible (Smoothly), such as extension of its duration (duration), avoid the use of bmp format image as background.
7. For the DataGrid complex ItemRenderer, try to use the Canvas instead of VBox or HBox, and Flex 1.5 this is different.
8. If the DataGrid's ItemRenderer a ComboBox, so as not to use ItemEditor ItemRenderer, only elected in the cell will display the Renderer (to avoid a one-time all the cell Renderer are drawn)
9. I remember at any time Listener Remove useless or use weak references, this mechanism in the event I mentioned (URL. ..); null unused variables and arrays.
10. For the use of large quantities of data when necessary to fetch or page
11. Advance statement frequently used variables, such as:
var l: int = list.length;
var w: int = stage.stageWidth;
for (var i: int = 0; i <l; i + +) (
list [i]. x = w -100;
12. This can improve the performance of Flex, probably many people will not bother, but at least it can reduce unnecessary for Flex in terms of computing to improve performance, I think this program really a matter of habit is not easy to change, at least I was 1 year or until recently aware of this issue.
In addition the proposals and code organizations, such as:
1) systematic constant write a constant with the const keyword class inside and some ItemRenderer code to be independent and not directly into a document written everywhere
2) Do not quote long list of elements in the interface of parent.parent.parent ..., once the interface re-organization will give you great trouble. If you use the Cairngorm framework, then it should ViewHelper, your code is loosely coupled as possible.
13. Modify the production-mode attribute is true, 2.01 in the file in the flex-webtier-config.xml. It generally is the product release time to set, you can be understood as c + + in Release and Debug versions of differences.
14. If you use FDS, then try to use the Remote Object and not Http Service or Web Service, because the former is the most efficient
15. On stress testing your system, Adobe has a testing tool, called the Flex Stress Testing Framework
It provides RPC in the RTMP, AMF, HTTP test, general commercial testing software does not support the first two tests,
The software is free, you can go to Adobe official website, the next (URL: http://download.macromedia.com/pub/labs/flexstress/flex_stressfw_p2_060407.zip)