as3 Greensock LoaderMax memory - actionscript-3

I have a bit trouble with LoaderMax memory occupy, i have a queue, and i am keep loading images depend on user's action. if they click load more and it keep load, but i would like to clean the memory which been occupied by the previous queue (i have remove all the children been added by the loading previously). is there a way i can do it?
the behavior like this.
load
(load the page 1)
load
(load the page 2 and clean the page 1)
queue = new LoaderMax({name:"mainQueue", autoDispose :'true'});
**
A very quick question, everything in the queue (images). Do they all
cache in the browser?
**

{ autoDispose:true } will not unload content, only call the LoaderMax instances dispose method automatically.
You can either call queue.unload() or queue.dispose(true) to remove your loaded content.
Also make sure you have removed any listeners from the children you added to the display to ensure they are garbage collected.
LoaderMax Documentation
To answer your quick question: depends on the browsers settings but probably. LoaderMax has cache-busting options if you are worried about that.

Related

How to implement Caching while loading SWCs dynamically

I'm loading dynamic loading of all my SWCs in my master SWF, in order to load master swf faster, however now I need to cache all my swcs in local machine to speed up things.
private function loadAssets():void
{
swcObj=new Object();
swcObj.swcPath='assets/swc/1.swc';
swcObj.className="Part_0_1";
swcs.push(swcObj);
swcObj=new Object();
swcObj.swcPath='assets/swc/2.swc';
swcObj.className="0_2";
swcs.push(swcObj);
swcObj=new Object();
swcObj.swcPath='assets/swc/3.swc';
swcObj.className="0_3";
swcs.push(swcObj);
}
Then I'm using this array to use all the classes in my project, but I have no idea how to cache these swcs for faster use, if anyone have idea, please share.
In fact, the browser does this pre-caching for you, you don't need to produce extra efforts. So, just load them normally and don't worry about caching. You can, however, motivate the user to increase their local browser cache in order to potentially lessen time spent on waiting while your assets are loaded, but this won't help should the user watch three tons of YouTube each day.
SWC files are not intended for dynamic loading.
They are static libraries that can be linked in a swf using
-include-libraries and library-path options of mxmlc or - since you seem to be using FlashDevelop - SWC Include Libraries and SWC Libraries in Project>Compiler options
. SWC's may hold code (classes), assets (symbols/bitmaps/sounds...) or combination of the two.
Loading assets dynamically is done through flash.display.Loader
you may use the Loader as a simple DisplayObject instance that you add on stage:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#includeExamplesSummary
, or use its ApplicationDomain as a library of Class definitions that will allow you to create instances at will :
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html#includeExamplesSummary
Caching from the browser will be sufficient in most cases, unless you have VERY specific needs
In the end, there is different ways to optimize loading times, one of which is having a small swf acting as loader/home menu, and loading rest of the content on demand like you seem to try doing, but you can also create a single swf with several frames, which will be "streamed" by flash player, example:
1st frame as small as possible holding just a few kb for a splash screen/logo/loading/whatever you want, to make the initial blank screen as short as possible, then second frame holding the main content. You can event extent this system with for instance
two levels of preloader: a first tiny frame with just a logo, second one with progress bar, and full background if needed
split content like home screen/menus in a frame, gameplay in the another, if you are making a game, so that gameplay continues loading while you are already displaying the menu

Limitations of Web Workers

Please bear in mind that I have never used Web Workers before and I'm having some trouble wrapping my head around them.
Here's an explanation of a simplified version of what I'm doing.
My page has links to various files - some are text, some are images, etc. Each file has an image showing a generic file icon.
I want the script to replace each generic icon with a preview of the file's contents.
The script will request the file from the server (thereby adding it to the cache, like a preloader), then create a canvas and draw the preview onto it (a thumbnail for images, an excerpt of text for text files, a more specific icon for media files...) and finally replace the generic icon's source with the canvas using a data URL.
I can do this quite easily. However, I would prefer to have it in the background so that it doesn't interfere with the UI while it's working.
Before I dive right in to this, I need to know: can Workers work with a canvas, and if so how would I create one? I don't think document.createElement('canvas') would work because Workers can't access the DOM, or am I misunderstanding when all the references I've found say they "can't access the DOM"?
You cannot access the DOM from web workers. You cannot load images. You cannot create canvas elements and draw to them from web workers. For now, web workers are pretty much limited to doing ajax calls and doing compute intensive things. See this related question/answer on web workers and canvas objects: Web Workers and Canvas and this article about using webworkers to speed up image manipulation: http://blogs.msdn.com/b/eternalcoding/archive/2012/09/20/using-web-workers-to-improve-performance-of-image-manipulation.aspx
Your simplest bet is to chunk your work into small chunks (without web workers) and do a chunk at a time, do a setTimeout(), then process the next chunk of work. This will allow the UI to be responsive while still getting your work done. If there is any CPU consuming computation to be done (like doing image analysis), this can be farmed out to a web worker and the result can be sent via message back to the main thread to be put into the DOM, but if not, then just do your work in smaller chunks to keep the UI alive.
Parts of the tasks like loading images, fetching data from servers, etc... can also be done asynchronously so it won't interfere with the responsiveness of the UI anyway if done properly.
Here's the general idea of chunking:
function doMyWork() {
// state variables
// initialize state
var x, y, z;
function doChunk() {
// do a chunk of work
// updating state variables to represent how much you've done or what remains
if (more work to do) {
// schedule the next chunk
setTimeout(doChunk, 1);
}
}
// start the whole process
doChunk();
}
Another (frustrating) limitation of Web Workers is that it can't access geolocation on Chrome.
Just my two cents.
So as others have stated, you cannot access the DOM, or do any manipulations on the DOM from a web worker. However, you can outsource some of the more complete calculations on the web worker. Then once you get your return message from the web worker inside of your main JS thread, you can extract the values you need and use them on the DOM there.
This may be unrelated to your question, but you mentioned canvas so i'll share this with you.
if you need to improve the performance of drawling to canvas, I highly recommend having two canvas objects. One that's rendered to the UI, the other hidden. That way you can build everything on the hidden canvas, then draw the hidden canvas on the displayed one. It may not sound like it will do much if anything, but it will increase performance significantly.
See this link for more details about improving canvas performance.

Force SWF (Flash file) to load LAST after rest of webpage

I have a rather complex flash clip that ends up bogging down the loading of the rest of my webpage.
Can I force the flash to wait to load until the rest of the site loads?
If you do not mind to have javascript you may use something like SFWObject 2 library, here is documentation page http://code.google.com/p/swfobject/wiki/documentation there is different methods for using it, so you just need pick up which is more appropriate in your case. Main idea you may trigger swf player initialization with javascript, so you could control time when this happen, and so using it just do that after onload event triggering. But assume problem cold be more complicated than just delaying your clip, so try to test it on different computers and browsers.

AS3 UILoader swf continues to play in the background

I have a simple expandable advertisement which loads an external SWF file. The external SWF file contains an embedded video.
Using the UILoader component, I have loaded the SWF, added it to the stage and the video displays and plays fine.
The main issue is that I have a close button, which should unload the video, and stop it, essentially returning to the default state of the advertisement.
To unload the SWF, I am doing the following:
myUILoader.unload();
removeChild(myUILoader);
When I do this, the video and UILoader disappear from the screen. However, you can still hear the sound in the background, and if I load the SWF again, it creates multiple tracks in the background. Any suggestions?
There are a couple of things that could cause this. One is that the content inside your UILoader is adding event listeners to outside of itself. You can fix this by not allowing it to touch anything outside itself. I believe you can do this by placing it either in its own ApplicationDomain, SecurityDomain, or both. Consult the help for more details.
If this is not feasible, you can try SoundMixer.stopAll(), which will stop the obvious symptom, but will not fix the memory leak you probably have in this situation.
Another possibility, as Ronnie has alluded to, is that you still have a reference to the content of the loader somewhere. If you don't clear that, it will stay in memory.
However, there is another problem that can also cause this, which is that if there is navigation in the movie that skips over a frame that contains a MovieClip with audio set to "stream," the MC will be created but not fully instantiated and will stay in memory with no way to get any control over it or release it. I don't think this is what is happening from your description. If it is, the fix is to make sure that you visit the frame that contains the sound, however briefly, on the way to the other frame. This is actually something you might want to consider even without sounds, because it does occur any time you skip frames in nested MovieClips (you just have no evidence unless you profile the swf), and over time this will create a memory leak.

Load SWF data without loading sound, then load sound later

So, hypothetical situation here:
I have an SWF that's 30MB. Sound files (music) make up 25MB, art and other things make up the remaining 5MB.
Would it be possible for me to load the 5MB of necessary art and other things first to allow the user to operate the app, then after that's all loaded and they are operating the app, load the remaining 25MB of sound files in the background?
UPDATE:
Loading SWF (or other entities) externally is not an option.
You can do this by modifying the compilation settings and strategically placing the sound.
By default, anything placed on the timeline will be loaded sequentially; the Flash Player will play the SWF as soon as the first frame is loaded. If you use the default compilation settings, all library content will be placed in the first frame, so the movie doesn't start until everything is available.
You can modify these settings, however, to allow for a more sequential load order: For each of your library elements, you can uncheck "export into frame 1" in the properties window. Now these elements won't be loaded, until they appear in the main timeline. This way, if you place your content carefully, you can allow for all important elements to be loaded in the first frame, or if you have a progress bar, until the main movie starts, while all streaming elements load with the animation, which has to be placed accordingly. Make sure though, that you don't leave anything out (by not placing it on the timeline), or call elements from ActionScript before they are loaded completely. It is very important to test this thoroughly, because if anything goes wrong in the load order, your entire SWF might stop working.
Also, remember that the SWF loads sequentially: If you have a sound in, say, frame 300, and another in frame 1000, the one in 300 will be loaded first. If you jump to frame 1000 from a menu in frame 10, you have to take into account that the frame might not be loaded yet. So there has to be some sort of checking mechanism (framesLoaded) and/or dialog to inform the user about additional loading time, and prevent the application from crashing.