Jump to content
Search Community

Questions regarding carbage collection and unloading

Ahrengot test
Moderator Tag

Recommended Posts

Today is the first time i've got to play around with LoaderMax, and it really feels like a fantastic tool to work with. It's very intuitive and easy to wrap your head around if you're familiar with TweenMax and TimelineMax. I can't wait to get it completely under my skin, as it actually makes loading stuff fun, which is very bizarre to me ;)

 

Here's my question: Let's say i have a LoaderMax with 100 ImageLoader's nested inside of it and i want to keep everything running smoothly without memory leaks etc.

 

Question 1: Unloading

What excactly does LoaderMax.unload() do? The documentation states that the unload method does the following "Removes any content that was loaded and sets bytesLoaded back to zero." does that mean content in nested loaders? how about content nested in loaders nested in another loader? For instance, could i call .unload() on my LoaderMax containing 100 image loaders and expect they'd all unload without having to set up for loops or anything like that?

 

Also, does .unload() cancel all current loaders or should i first call .calcel() and then .unload() if i want to abort all loading and destroy my LoaderMax instance?

 

Question 2: The autoDispose feature

Am i mistaken by thinking this is essentially a kind of remove-all-EventListeners method? It's turned off by default, but wouldn't i normally want my loaders to remove their listeners when they're done loading? Also, what happens if i forget to dispose of a loader when i'm done with it? Will it break my site/application or are those kinds of things handled?

Link to comment
Share on other sites

Today is the first time i've got to play around with LoaderMax, and it really feels like a fantastic tool to work with. It's very intuitive and easy to wrap your head around if you're familiar with TweenMax and TimelineMax. I can't wait to get it completely under my skin, as it actually makes loading stuff fun, which is very bizarre to me ;)

 

Ha ha. Glad to hear it's workin' well for you.

 

Question 1: Unloading

What excactly does LoaderMax.unload() do? The documentation states that the unload method does the following "Removes any content that was loaded and sets bytesLoaded back to zero." does that mean content in nested loaders? how about content nested in loaders nested in another loader? For instance, could i call .unload() on my LoaderMax containing 100 image loaders and expect they'd all unload without having to set up for loops or anything like that?

 

Good question - I'll clarify this in the documentation. When you unload() a LoaderMax instance, it will also unload() all of its children. You do not need to manually loop through the children and unload() each one. That also means that it unloads all nested loaders as deeply as they go.

 

Also, does .unload() cancel all current loaders or should i first call .cancel() and then .unload() if i want to abort all loading and destroy my LoaderMax instance?

 

Yes, if you unload() a loader, it will automatically cancel() it too if it is in the process of loading.

 

Question 2: The autoDispose feature

Am i mistaken by thinking this is essentially a kind of remove-all-EventListeners method? It's turned off by default, but wouldn't i normally want my loaders to remove their listeners when they're done loading? Also, what happens if i forget to dispose of a loader when i'm done with it? Will it break my site/application or are those kinds of things handled?

 

No, you generally wouldn't want loaders to be automatically disposed because you very well may need to access them later to find their content or to figure out the URL or maybe you want to query the system to find out if a particular loader is complete, etc. Once a loader is disposed, you can no longer find it with LoaderMax.getLoader() or LoaderMax.getContent(). The new VideoLoader class has some really useful methods and properties that make it perfectly capable to be used as a simple video player itself, but if you dispose of the VideoLoader instance, you can't use its methods/properties (like pauseVideo(), resumeVideo(), seekVideo(), bufferProgress, playProgress, videoTime, volume, etc.).

 

And by the way, when a loader is disposed, it does remove the event listeners that were added through the vars object (like {onComplete:completeHandler, onProgress:progressHandler}) but it doesn't know what other ones you added manually so it doesn't remove those.

 

For the record, I have spent a lot of time trying to make sure garbage collection is handled as thoroughly as possible. For example, swfs are notorious for not unloading properly but there are some workarounds implemented inside LoaderMax to mitigate this problem. Same with images that are canceled before fully loading, and there are some tricks to working with a NetStream and getting it to garbage collect properly (and not get mistakenly garbage collected). The LoaderMax system should ease a lot of the "typical" burdens that developers wrestle with in terms of loading.

 

Don't forget to get the latest version and try hard to break it before I release v1.0 to the public :)

Link to comment
Share on other sites

Thanks for that elaborate reply, Jack!

 

Just to clarify, all i really need to do, to garbage collect my LoaderMax along with it's 100 ImageLoader's is to call the unload() method on my LoaderMax instance and nullify it like:

 

_loaderMax.unload();

_loaderMax = null;

Link to comment
Share on other sites

Almost - you need to dispose() of it as well. When you unload(), it dumps the content but keeps the loader in case you want to load() again (or whatever). So to remove all content and dispose of all the loaders in your LoaderMax instance, you should do:

 

_loaderMax.unload();
_loaderMax.dispose();
_loaderMax = null;

Link to comment
Share on other sites

I'm not sure if this should go in a separate thread but in v9 AS3 if you load a SWF and that SWF loads a sound and then unload the SWF the sound keeps playing. I've only heard that you'd have to have access to the SWF code itself to make sure to shut down net connections and stop the sound there in order for it to really be unloaded. v10 added some functions to make that easier IIRC. Any thoughts?

Link to comment
Share on other sites

Is that happening for you in LoaderMax or are you just saying in general Flash has a problem unloading swfs? I have tried a swf with audio playing on the first frame and I put code in LoaderMax to brute-force unload that sucker. Of course it's not always possible to get around bugs in Flash so I can't guarantee it'll work perfectly in every situation, but LoaderMax seemed to be working well for me. If you have an example FLA/SWF that demonstrates LoaderMax not unloading something properly, would you please send it to me?

Link to comment
Share on other sites

That was a flash thing in general. I'll have to put together a standard sample when I have some time and then try with LoaderMax to compare the two. The audio in this particular case was streamed so the net connection couldn't be closed from the outside and we didn't have access to the swf which was a game. The brute force method we used in this case was loading the swf in a div via jquery and destroying that div to unload it. Yikes!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...