Accessibility

Table of Contents

Memory management and optimizations in Flash Lite

Fragmentation and memory reuse

As with any other memory management system, fragmentation occurs in Flash Lite when an application allocates and frees memory. When a garbage collection cycle completes, there are usually some "islands" of free memory in the heap leading to what is called as fragmentation. The fscommand2 ("GetFreePlayerMemory") command returns the largest memory that can be allocated. It does not include the memory that is freed by the garbage collector. So, there is (almost always) some additional memory (if freed by the garbage collector) that can be reused by new objects. The following illustrates the concept:

Suppose, for a particular device, the manufacturer defined 256 KB of static memory and 512 KB of dynamic memory.  Suppose also that at some instant in the timeline, all the static memory is used up and about 256 KB of dynamic memory is used up by the rendering objects. All that is left is 256  KB of dynamic memory.  So, the memory heaps looks something like Figure 2:

Example of the memory heaps showing the conditions listed above

Figure 2. Example of the memory heaps showing the conditions listed above

  1. Create two objects that consume 40 KB and 30 KB respectively. The player will allocate 64 KB for first object and 32 KB for second [Since memory from OS is allocated in 32  KB blocks]. The fscommand2 ("GetFreePlayerMemory") will return 160 KB (256-96)  KB.  The memory heaps look something like Figure 3:

    Example of the memory heaps showing the conditions listed above

    Figure 3. Example of the memory heaps showing the conditions listed above

  2. Create a third object to consume 160 KB. fscommand2 ("GetFreePlayerMemory") should now return 0  KB. Now, the memory heaps look something like Figure 4:

    Example memory heap showing the conditions listed above

    Figure 4. Example memory heap showing the conditions listed above

  3. Delete the first two objects. After the garbage collection cycle, even though there is 96  KB [in 64 KB and 32  KB chunks] available, fscommand2 ("GetFreePlayerMemory") still returns 0  KB. However, you can still reuse the 96  KB freed up in following ways:
    1. 3 objects, each of size 32  KB or less
    2. 2 objects, where, 1 object can take up to 64 KB and another 32  KB.

    Example of the memory heaps showing the conditions listed above

    Figure 5. Example of the memory heaps showing the conditions listed above

Note that the above example is valid only if the device provides the player with dynamic memory. In case of the static memory heap, exact memory is allocated (not in 32 KB chunks). So, the memory can be reused only when a new object requires memory of size equal or smaller than what got freed in static memory heap.