I’ve noticed something strange. When monitoring a loader’s progress, if I get bytesLoaded from the loader’s contentLoaderInfo it works just fine, but the bytesLoaded property of progress events just keeps going up when I load subsequent files.
Try this example. When I press escape to exit out of a loaded swf the second time, the progress text has gone above 100% (because I used e.bytesLoaded instead of loader.contentLoaderInfo.bytesLoaded).
You really shouldn’t be reusing Loaders. It looks like you’re just keeping a single one around for the entire duration:
package SWFbrowseandload_fla {
public class GameContainer_3 extends flash.display.MovieClip {
public var curtain:flash.display.MovieClip
public var loader:flash.display.Loader
public var escMsg:flash.display.MovieClip
So just swap out to a new Loader at some point. Maybe right after you unload in quitGame.
(Note: It’s been a long time, so I don’t know where my internal “don’t reuse Loaders” advice came from. But it’s not a hassle to make new one each time… so why not do it?)
Sounds like good advice. You are quite right. I am reusing a loader. I try to conserve memory usage which means minimizing the amount of objects I construct, and constructing too many objects can also slow things down if you’re not careful, and since the Loader class has an unload method I figured that means there’s no reason not to unload and reload with the same loader. But if you say that causes problems I’m inclined to believe you.
Sure, but a single Loader instance that’s global to your application is going to take up like 100 bytes, which is like 0.0000025% of the memory capacity even on tablets and phones these days. It’s the size of the content you load that matters. Plus, swapping out the loader would only happen when the user clicks to load up a new game. So even if they played 1000 different games and the garbage collector broke, that’d still only be ~100KB of wasted memory.
That’s solid reasoning. There’s no reason from the outset to think that Loaders are going to have some weird bug when dispatching ProgressEvents, and that’s Adobe’s fault.
Sorry to resurrect the thread, but I read on another website that Loaders don’t get garbage collected. Is there any truth to this?
The Stage is a GCRoot. Loaders are GCRoots. Certain menus are GCRoots. Every object that is still in use by the application is reachable from one of the GCRoots within the application. GCRoots are never garbage collected.
It may be true; I don’t know. Sounds plausible since it’s coming from Adobe.
Again, though, that’s talking about the Loader object itself, not about content that the loader loads.
Flash isn’t really built to make it memory-friendly to have a long-lived container swap apps/games in and out a bunch. So if you’re trying to, say, build a kiosk that runs for months on end loading new things in and out all of the time, your best bet to avoid running out of memory is to reload the outer Flash container every once and a while. unloadAndStop was added as a way to try to make it easier to clear out loaded content, but it’s not perfect.