Jump to content
Search Community

Clearing a queue of DataLoaders & Freeing up memory

ukmikeb test
Moderator Tag

Recommended Posts

Hi,

 

Just need to sanity check what I am doing and get best advice on clearing loadermax queue (and contents) from memory.

 

So, I am using a LoaderMax queue in Air to download & store locally a series of assets (images, videos, pdfs).

 

_queue.append(new DataLoader(itemURL, {name:itemID, format:"binary", estimatedBytes:_estBytes_general, autoDispose:true}) );

 

I should say at this point I am not yet doing the saving to local filesystem (so that is not influencing memory)

 

So, Potentially I may have 100's of MB of files which may kill the app if I wait until the end of the queue. So as a queue "child" loads in, I am (going to) save to file, then I kill of the child & it's contents.

 

I also call garbage collection at this point (Air only).

 

function queueChildComplete(event:LoaderEvent):void {
event.target.unload();
_queue.remove(event.target as LoaderCore);
System.gc();
}

 

When the entire queue completes, I then call the following to clear the queue from memory (not that their should be anything in there anymore).

 

_queue.unload();
_queue.dispose(true)
_queue.empty(true,true);
System.gc();

 

Don't think I need to be calling all of those unload, dispose & empty.. but you know belt & braces!

 

Now the problem is, when I trace System.privateMemory before (just before queue.load) & after the operation, memory has increased by the size of the files downloaded.

 

1) What is the text book method of disposing of a queue, and it's contents .. totally from memory (knowing that I will have re instantiate it later).

 

2) Do you have a handy tool that will stream files directly to the local filesystem for AIR apps (using urlstream perhaps).

 

Many Thanks

Link to comment
Share on other sites

You should only need to call dispose(true) to completely obliterate a loader, FYI.

 

As for memory increasing, did you maybe store a reference to the content somewhere? If so, that would explain why gc wouldn't wipe it out. Other than that, I'm not quite sure what to say (tough to troubleshoot blind). And I don't know of a tool that will stream files directly to the local file system for AIR apps - sorry :(

Link to comment
Share on other sites

Hi,

 

Thanks fro responding over the weekend..

 

OK, so dispose(true) I will do that, thanks!

 

As far as holding a reference to the content goes, I don't think I am as currently I am just downloading & then immediately disposing of the content (my NEXT step is to save the content to disk, but have yet to add that).

 

So, is it possible that the fact that I am immediately disposing of the content while the queue is still running, could be upsetting things?

 

I will try some cleaner simpler tests myself.

 

Thanks for your guidance anyway.

Link to comment
Share on other sites

OK a quick follow up to my last message..

 

It would appear that Flash/AIR is not the best at Garbage Collection. ;)

 

My tests tell me that you need to call system.gc() (upto) twice to get it to kick in.

 

You also need do this a frame apart, so you need to set up an ENTER_FRAME event to handle these multiple garbage collection requests.

 

Ref: http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/

 

However, now that is sorted there does seem to still be an issue with removing items from the queue individually and freeing up memory.

 

If I just let the entire queue complete and call GC as above.. no problem.

 

However, I don't want memory in the queue to get out of hand so, I am saving to disk & killing queue items as they complete:

 

function queueChildComplete(event:LoaderEvent):void {
saveToDisk();
event.target.unload();
_queue.remove(event.target as LoaderCore);
requestGC(); // garbage collection (as above)
}

 

I then go on to try and remove what remains of the queue with:

 

private function queueComplete(event:LoaderEvent):void { 
_queue.dispose(true);
requestGC(); // garbage collection (as above)
}

 

Now, I am not actually saving to disk yet, and just testing the theory of queuing, downloading & removing.

 

Is it possible that I should also wait a frame before trying to remove an item from the queue?

I of course can't be sure how long the writing to disk process will take, as I imagine if a file exists for example, saving will be instant.

 

Any thoughts on queue item removal would be great.

 

Thanks, and hope the garbage collection info may be of some use...

Link to comment
Share on other sites

Further update...

 

Adding autoDispose:true means children & queue being liberated from memory!!! Hooray!

 

_queue.append(new DataLoader(_itemURLS[i], {name:"item_"+i, format:"binary", autoDispose:true}) );

 

Out of interest, in what case would you not want autoDispose:true? and how do you "manually" dispose of individual DataLoaders in a queue?

 

If I have set autoDispose:true, does these mean I can no longer gain access to the content?

 

thanks..

Link to comment
Share on other sites

Out of interest, in what case would you not want autoDispose:true?

If you ever want to be able to reference that loader later. For example, many developers like to load stuff and then use LoaderMax.getLoader(...) or LoaderMax.getContent(...) to get their stuff, but if you dispose of the loader, you of course wouldn't be able to find it later. It is actually rather rare that folks would want autoDispose:true. Also, think about a VideoLoader that allows you to control the video after it is loaded - if you dispose of the VideoLoader instance, you lose your ability to control the video through that instance.

 

and how do you "manually" dispose of individual DataLoaders in a queue?

Simply call that DataLoader's dispose() method. Pretty simple. That removes it from the queue as well.

 

If I have set autoDispose:true, does these mean I can no longer gain access to the content?

No, autoDispose only disposes of the Loader instance itself, not the content that was loaded. Those are distinct concepts. For example, if you have an ImageLoader whose container is this.stage and autoDispose:true, it will load the image onto the stage and then immediately dispose of the loader instance but the image would remain on the stage.

 

Have you read through the ASDocs? Those might be helpful as well as the "tips & tricks" page: http://www.greensock.com/loadermax-tips/

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...