This Blog focuses on the implications that some of the new ActionScript features have on resource management, and looks briefly at some of the new tools in ActionScript that can help you to track and manage memory more effectively.
The growth and stability of a system is limited by the scarce resources. In the case of Flash, there are typically bandwidth , CPU and memory.
There are generally two focuses for managing resources:
Optimization:
Reducing resource usage.
Reclamation: Freeing inactive resources for collection.
Why is Resource Management an issue?
Issue 1: Dynamic content
One of the more obvious issues encountered with resource management is related to sprites (or other display objects) that you instantiate dynamically, and then wish to remove at a later time.
Consider a movie clip that follows the mouse by subscribing to the Stage's mouseMove event.Unless you remember to remove the listener, the clip will continue to execute code every time the mouse is moved, even after the clip is "deleted." By default, the clip executes forever because a reference to it exists from the Stage for event dispatch.
Issue 2: Loaded content
Remember that the contents of loaded SWFs are also now treated the same way as every other object and you can begin to imagine some of the problems that you could encounter with loaded content. Similar to other display objects, there is no way to explicitly remove a loaded SWF and its contents from memory, or to stop it from executing. Calling Loader.unload simply nulls the loader's reference to the SWF ; it will continue to exist and keep executing until it has been picked up by the next garbage collection sweep.
Using System.totalMemory
Although System.totalMemory is a simple tool, it is important because it marks the first runtime profiling tool that developers can use in Flash. It allows you to monitor how much memory is in use by Flash Player at runtime.More importantly,it makes it possible for you to preemptively deal with major memory leaks in your content before it could cause a serious issue for your user. It's always better to throw an error and abort your application, rather than bog down the user's system or even stall it completely.
Here's a simple example of how you could accomplish this:
import flash.system.System;
import flash.net.navigateToURL;
import flash.net.URLRequest;
... // check our memory every 1 second: var
checkMemoryIntervalID:uint = setInterval(checkMemoryUsage,1000);
... var showWarning:Boolean = true;
var warningMemory:uint = 1000*1000*500;
var abortMemory:uint = 1000*1000*625;
... function checkMemoryUsage()
{
if
(System.totalMemory > warningMemory&& showWarning)
{
// show an error to the user warning them that we're running out
of memory and might quit
// try to free up memory if possible
showWarning = false; // so we don't show an error every second
}
else if (System.totalMemory > abortMemory)
{
// save current user data to an LSO for recovery later?
abort();
}
}
function abort()
{ // send the user to a page explaining what happpened:
navigateToURL(new URLRequest("memoryError.html")); }
This could obviously be enhanced in a number of ways, but hopefully this code demonstrates the basic concept behind this process.
0 Comment(s)