Accessibility

Table of Contents

Memory management and optimizations in Flash Lite

Optimizations for efficient use of memory

  1. Avoid loading and unloading images, objects, and external data as a continuous set of actions. This will not let the garbage collector complete its scheduled cycle and the application may run out of memory. For example, with three files of the same size but with memory available only for two:
    1. Load  file 1
    2. Remove file 1
    3. Load  file 2
    4. Remove file 2
    5. Load file 3
    6. Load may not be successful
  2. The above scenario is not recommended.

    1. Load file 1
    2. Remove file 1
    3. Wait for about 60 sec ( use setinterval)
    4. Load file 2
    5. Remove file 2
    6. Load file 3
    7. Load will most probably be successful.

    In this case the GC works in between steps "b" and "d" and the memory is reused.

  3. Delete variables or set them to null when you no longer need them. Doing this marks the data for garbage collection. Deleting variables helps optimize memory use during runtime, because unneeded assets are removed from the SWF file.

    The following two code examples show how to free memory that objects use by deleting the variable that references those objects. The examples are identical, except the first example creates a timeline and global variable and the second creates a local variable.

    // Global and Timeline Variables:
    // variable attached to a movie or movie clip timeline
    
    var mcDateObject = new Date(); // Creates the Date object.
    trace(mcDateObject); // Returns the current date as a string.
    delete mcDateObject; // Deletes the object.
    trace(mcDateObject); // Returns undefined.
    
    // global variable attached to a movie or movie clip timeline
    _global.gDateObject = new Date(); // Creates the Date object.
    trace(_global.gDateObject); // Returns the current date.
    delete _global.gDateObject; // Deletes the object.
    trace(_global.gDateObject); // Returns undefined.

    As mentioned previously, you can't use the delete statement to free memory that a local function variable uses. Instead, you can set the variable reference to null, which has the same effect as using delete. The following code example demonstrates this.

    // Local Variables:
    
    function func()
    {
    var funcDateObject = new Date(); // Creates Date object.
    trace(funcDateObject); // Returns current date(string).
    delete funcDateObject; // No effect.
    trace(funcDateObject); // Still returns current date.
    funcDateObject = null; // Sets object reference to null
    trace(funcDateObject); // Returns null.
    }
    
    func(); // Calls func() function.
    
  4. Explicitly remove listeners from objects by calling removeListener( ) before garbage collection.
  5. Limit the use of global variables, because they are not garbage collected if the movie clip that defined them was removed (As shown in optimization tip #2 above).
  6. Avoid including embedded fonts whenever possible. Embedding a font will embed the complete font file in SWF itself not only increasing the size of SWF but also taking up the static memory. Further, the increased SWF file size will increase the time required to launch the application and the download time if you plan to deliver the file over the air (OTA delivery).
  7. Use LoadMovie / Unloadmovie to inform the GC about freeing memory.
  8. With incoming XML data from a server, large amounts of XML should be broken up before loading into the player if possible. If you are manipulating a lot of data on the client player, consider using the server to pre-filter the data.  
  9. When one SWF file loads another SWF file that contains a custom ActionScript class (for example, foo.bar.CustomClass) and then unloads the SWF file, the class definition remains in memory. To save memory, explicitly delete any custom classes in unloaded SWF files. Use the delete statement and specify the fully qualified class name, for example : delete foo.bar.CustomClass.

The tips and procedures I've included in this article should help you better manage the memory requirements of your Flash Lite content.

Where to go from here

Follow these links for more tips on creating content for mobile devices:

Flash Lite bitmap and vector graphics in mobile devices

Optimizing ActionScript for Flash Lite content on mobile devices

Loading data for mobile devices in Flash Lite