Flex delayed instantiation

Source: Internet
Author: User

I.

As Flex is increasingly known, more and more Internet applications have begun to use RIA. As we all know, broadband applications in China are not as developed as in many developed countries. The bandwidth of individual applications is generally below 2 MB, how to make your Flex application run smoothly on the client has become a major challenge restricting every Flex application development programmer.
Here, I have collected and sorted out some experiences on the Internet.

Basic Principles:
1. Load Media from the outside)
     Heider mentioned a common Flex best practice-limiting the number of media embedded into an application/SWF file. resources such as videos and mp3 files can be loaded from external SWF files.
The Flex framework can directly compile images, mp3 files, fonts, and other resources into SWF. This method can be used when you want end users to get all the resources, but it will cause your application to stay in the "Loading" phase for a long time.

2. Restrict character sets in embedded fonts
Heider recommends limiting character sets in embedded fonts to reduce the total download time of SWF files:
When you embed a font in Flex, you will get support for all characters in the font. Although this may be what you want, are you sure you want all the characters? For example, in an English-only application, are you sure you want to take the time to download Chinese character data?
3. cache framework
       Heider reviewed the Flex 3 support for runtime-shared-libraries (RSL) article:

From Flex 3, You Can cache the Adobe signature framework-RSLs to the cache of Flash Player. There are two benefits. First, the Framework RSLs of the signature cached in the Flash Player cache can be shared by all configured Flex applications. In other words, if someone's application has downloaded the K signature framework RSL and the RSL is still in the Flash Player cache, then your application can use the cached RSL. Second, even if someone clears the browser cache, there is no impact on the Flash Player cache.
4. Modular
Heider talked about the benefits of dividing a Flex application into modules: Another way to reduce the font loading time is to divide your Flex application into modules. One advantage of using a module is that you can fully control it when loading and detaching a module.
The last reason for dividing them into modules is that they are faster and I can load them instantly. In other words, the only module to be loaded at startup is the Step1.swf module. Therefore, when a module is used, the end user saves start time, but it takes more time to switch from one module to another, because each module needs to be loaded in JIT format. In my applications, it takes more time to switch between steps 1-5 for the first time.
5. Deferred instantiation
     Heider provides many suggestions on the "creationPolicy" attribute of the Flex component and the different parts of the app to be instantiated.
     If you want to reduce the total time that can be used from data downloading to users, it is imperative to postpone instantiation. The idea behind this technology is to create objects in the memory until the application is actually used.
     Although the deferred instantiation technology can cause a little-usually less-obvious-latency throughout the application's use process, it is acceptable compared with a long start delay. Another benefit of deferred instantiation is the optimization of memory usage.

The above principle comes from Jun Heider's excellent article published on the InsideRIA site of O 'Reilly. This article provides many suggestions on how to speed up the startup of Flex applications, to help you reduce the time when you see the annoying "Loading" dialog box. He discussed different aspects of the problem in depth and judged the advantages and disadvantages of each technology. Heider also talked about an "experimental" term-"Use stream", which he talked about when discussing Dirk Eismann's post (Building monolithic Flex SWFs that still startup quickly. Eismann proposed a technology to use multiple frames in Flash Player to achieve stream in some applications. View all the posts to learn more about the technology and suggestions on accelerating Flex startup.

Memory release optimization principles
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 remember to 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. In addition to 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 deleted after being automatically deleted by the system or during the second removal 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 the registered event is not removed, it does not affect the custom forced collection mechanism, but it may affect the normal collection mechanism. Therefore, it is best to ensure that all registered event listeners should be deleted.
9. if the parent object is deleted, it does not mean that all other sub-objects are deleted. If the leaked code in one State is not in another state, it will not be leaked. test and analyze each state of each module one by one, the entire object can be deleted in any State of the test.
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 class leakage: You forgot to delete the system class, 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 the Effect of Effect is applied to the component, when the object is deleted, the Effect animation on the object and the sub-object needs to be stopped, and then the target object of Effect is set 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: After the Image object is used, set source to null (for testing );
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.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/justdo2008/archive/2009/09/11/4544632.aspx

 

II.

The longer Flex is, the more important the performance issue is. This is a matter that programmers should pay great attention.
   I think there are several points about Flex performance:
   I. FLEX file volume problems.
   The file size generated by Flex is relatively large. This is the biggest headache! If the file size is large, the user will wait for a while browsing. This alone will greatly compromise the user experience. Maybe your application is Cool, however, users may leave without seeing your application, except for loyal Fans or technical researchers.
   How can we reduce the size of a file? I personally think it can be divided into two aspects:
   First, optimize the program itself: use as few images as possible, even if you use the image, you must first optimize the image without affecting the browsing quality (for how to optimize the image, refer to other related articles); do not place some sequent data in the program, it can be read through an XML file. Try not to include other files in the program, and Load the files into the program.
   The second is to separate the Runtime Library and use RSL to reduce the volume of files generated by FLEX.
   The procedure is as follows:
   1. Right-click the project folder and select "properties"-"Flex BuildPath"-"Library Path"
   2. On this tab, we can see "FrameWork linkage". By default, "Merged into cdoe" is selected"
   3. Open the drop-down list and select "runtime shared library (RSL)". Click "OK"
   In my personal experience, a file with more than 450 KB is converted into more than 140 KB after RSL, and the file size is significantly reduced. In the bin-debug directory, we found two more files: framework_3.1.0.2710.swf and framework_3.1.0.2710.swz;
The general principle is: when the client has a Runtime library, it will not be downloaded from the server. If there is no Runtime library, it will be downloaded from the server.
Note: When uploading files to the server, you need to upload the two files generated together with the Project swf in the same directory.
  There are three main causes for RSL error:
    1. Use flash player earlier than 9.0.115 ,,
    2.failed to upload framework_3.1.0.2710.swf and framework_3.1.0.2710.swz to the server, so that the class library could not be loaded.
    3. You may also put the file on the server, but your server does not support the swz format. Take IIS as an example to add a MIME type. swzapplication/x-shockwave-flash
   Ii. Flex Performance Optimization
  1. as3. write as3.
  2. When writing a variable, stick to the declaration type, minimize the definition of member variables and member methods, and use local variables and methods more.
  3. Too much code is not processed in the createComplete stage of the control
  4. Create as few instances as possible.
  5. Containers should not be nested too much. It is best not to have more than three layers. Empty spaces are filled with spacer.
  6. The object location and size are controlled by code to reduce the computing load on the computer.
  7. Avoid common container redundancy. Such as vbox in panel.
  8. Deferred program instantiation. When page Jump is required, the navigation bar container ViewStack, Accordin, and TabNavigator in state. Flex can be used as much as possible to postpone instantiation.
  9. If style is involved, use CSS files as much as possible.
  Iii. Performance Optimization in combination with FLEX and 3D.
    I have been working with flex and 3D recently, using papervision3d. The key lies in the reading of the 3D model and the control of the action. The following describes how to optimize the program:
    3D file model. Files in. DAE and. MD2 formats are supported. These two formats of files have a skeleton animation, which allows you to control the model action. DAE-format actions are mainly controlled by frames, while MD2 can be controlled by action names. (After research, we found that DAE is also controlled by action names, and additional class packages are required through frame control)
  Regardless of the file format, pay attention to the following when creating a model: do not make the model too fine. The more faces, the slower the loading. The most important thing is to load the 3D model to flex, tens of thousands of faces are no longer working. The quality of 3D models can be compensated by textures with higher quality.
 In addition, we will discuss the quality and quantity of textures. Minimize the image quality without affecting the visual effect. Make a model map as much as possible (the MD2 file does not seem to support multiple maps ).
    I have written so much for the time being, so I can continue with my thoughts.

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.