All about RIA development using Flash, Flex, Flash Media Server (Flashcom) with various middleware and backends.
9 Feb
We are working on a complex product line for one of our client. The key issue was to load different products / SWF applications into a single html page. It was causing increment in memory used by the browser in the system even if one unloads the loaded SWF from loader. Technical reason behind this was Garbage Collector (GC) was not cleaning its content from it while you unload the SWF from loader. This stuff was working fine previously with AS2.
As AS3 is more powerful in terms of performance and all, it also has some strict rules to follow to gain all the new AVM benefits. Loader will not unload the loaded SWF unless it completely stops doing any activities or listening / broadcasting events. In AS3 there is also changed mechanism of receiving mouse move events on any movieclip. Now we require to obtain such events from stage instead of particular movieclip.
The only workaround to this issue is a trick mentioned by Grant Skinner, which is to initiate new LocalConnections with same name twice. As it will generate error, we require to use catch.
[AS]
try {
new LocalConnection().connect(’foo’);
new LocalConnection().connect(’foo’);
} catch (e:*) {}
// the GC will perform a full mark/sweep on the second call.
[/AS]
Thanks Grant, for finding the catch and Adobe should do something about this issue J
10 Responses for "AS3 Nightmare: Unload loader content"
I always use weak reference for eventListeners (the last param set to true):
addEventListener(eventStr, eventHandlerFunc, false, 0, true);
This makes the Garbage Collector go round.
Is it even necessary to force gc once you have removed all your references.
Why not just wait until it runs normally?
If you think Adobe should do something about an issue, you need to provide valid test cases to help them understand your concerns. If you feel the garbage collector isn’t running properly, build an example that demonstrates a reproducible memory leak or memory that balloons out of control when you are sure that all references have been removed.
You mentioned listening/broadcasting events. Be sure to remove event listeners when they aren’t needed any more. That should be obvious. In cases where you need to listen to events from the stage, always use weak listeners. In fact, if you’re listening for events from object FOO in object BAR and BAR doesn’t directly manage the use of FOO (think subcomponents and similar fully-contained items), then you should be using weak listeners so that FOO doesn’t keep a reference to BAR because BAR is still listening to FOO’s events. Again, the most obvious example is the stage.
@Cedric
I will try to use that and see if that helps and thanks for your feedback
@Bjorn
It actually require as I need to load another SWF application in place of one. If I won’t force it much, It will use some memory and while I load another application; It will double the memory usage instead of remaining at same level.
@Josh,
Yes you are right, that We should give Adobe some test application which they can see and do something positive to solve issue in next release of flash player.
About events, We are removing events but still movieclips are not being removed from the memory. In some cases we even noticed that it doesn’t remove child movieclips while you remove the parent and they are not listening to any events anymore.
I will update you guys if we find any workaround than this because in our last tests we made on the code, this trick is even not helping. Just making sure out of 1000+ classes and all movieclips in library to make sure nothing else is affecting it.
u can solve this by doing this: - only tested with Loaders that loads Images -
(myLoader as Bitmap).bitmapData.dispose(); //DELETE all pixels from memory
myLoader.unload();
Uooth is one the fastest growing websites an the internet. Registrations is fast, simple tale is only a minute or two and then you ve an your way to being part of one of the greatest Uooth you will be able
to upload photos, videos, music leave comments on members profiles, write your owe blog, search for friends our make new one make your own profiles chat with friends discuss ideas calculate love This Great Site
Hi there- I came across your post looking for a solution to something else, but I had run into a similar issue to yours before.
Are you playing audio, video, or streaming anything in your child swfs? Those should be closed in addition to removing your listeners.
You could also try using external interface to add and remove different swfs from the DOM, bypassing this whole loader issue.
redblind,
sorry for replying a late. But we are loading Applications or say modules as SWFs.
Can you tell me bit more about the DOM option or any links?
Thanks for the help.
Leave a reply