I'm running my PlayN-game in the browser. In my task manager I see how the memory of google chrome or firefox is getting bigger and bigger up to 512 MB.
The probable reason is that I don't clean up and destroy all my elements that I use in playn.
I didn't clean up anything since the GarbageCollector was doing that in java for me and I didn't think about getting problems in HTML5.
So what is the easiest way to clean up everything (IFaces, Layers, Images...) after using it?
Can I just destroy the iface or the top layer and PlayN destroys all subelements for me?
If I destroy an ImageLayer, the underlying Image is probably not destroyed, right?
Sorry for the unspecific question. I only need to understand the general concept and best practices for it.
For the most part, the only thing you need to do is not retain references to objects that you no longer need. Garbage collection will take care of the rest for you.
You can call Layer.destroy to potentially speed up the freeing of GPU memory, but even that will be freed when the handles are garbage collected. In TriplePlay UI, Interface.destroyRoot removes the root from the UI (which you need to do if you're going to keep the Interface around but no longer need that root) and destroy's the root's layer.
The problem you may be running into is that the HTML backend currently caches all assets for the lifetime of your game. So if you load a lot of images or other data, that will eventually use a lot of browser memory. Any given image will not be loaded more than once, but perhaps you have a lot of images in your game.
I'll be changing this cache-whether-you-like-it-or-not policy before the next PlayN release, so you'll have to cache images on your own if you need them cached, but at least you will also be able to free them when you need to.
Related
My Flex web application is almost ready for launch. I'm currently optimizing as much as I can in order to make sure even low-end clients are able to run it fluently. The problem is, I have no idea where to optimize more than I've already optimized.
My question is, what are the usuals suspects regarding bottlenecks? What tips and experiences do you have when it comes to Optimizing a Flex app?
There are some web tutorials on this, this presentation looks helpful. And of course, the Flex profiler could help you identifying the bottlenecks.
That's a good presentation covering a lot of pitfalls. Another thing I can think of is over-usage of bindings in large applications. They're easy to create and forget, silently generating a lot extra code running in the background, and are especially potent when used carelessly on entire classes.
These are some of the things I've seen that cause slow downs. I've only worked with one or two Flex developers, so I don't know if this is obvious, or helpful. Either way:
Transparency: Whenever views have a lot of components with non-opaque transparency (i.e. alpha less than 1.0), these views tend to be extremely slow, especially on older machines.
Object Creation: It seems that whenever there is a lot of object creation happening, the app slows to a crawl, even on newer machines. I worked on an app that created at least a few hundred objects when the application started.
Data Fetching: Whenever data is loaded asynchronously, the app will take a performance hit depending on how much is loaded
Lots of objects: When views have lots of objects in them, they tend to lag and generally drag ass.
Those are the ones I remember the best, having not worked on a major Flex app in over a year.
Other than performance improvements you can optimize the file size of flex applications.That will definately improve user experience.I hope this article helps:
http://askmeflash.com/article/9/optimize-flex-swf-filesize-performance-loading
I have one swf file in that i used fscommand to get final output when submit button clicked in that swf ,
i am loading that swf in SWFloader in flex3 .i need to get fscommand value as Alert, how to get that value first and display as alert.
Thanks in advance
fscommand cannot be used for communication between loaded and containing SWFs.
From livedocs
fscommand: Lets the SWF file communicate with either Flash Player or the program hosting Flash Player, such as a web browser. You can also use the fscommand() function to pass messages to Director or to Visual Basic, Visual C++, and other programs that can host ActiveX controls.
You can call a method in the loaded swf OR access its properties directly OR use events OR use local connection to pass data between parent and loaded SWFs.
TECHNICALLY this is feasible, but a bad idea. You'd need to register a callback which would call the child swf (generally done from the child swf). But, you will risk a good deal of headache, and you'll have to rely a lot more on the browser than other options. It would also be slower than an AS only solution.
You're much better off (in this order):
Using a shared Singleton. This allows for complete separation of the two
swf's without requiring any major coordination between the two. The only
real potential problem can be caused if you want the child swf to have its
own ApplicationDomain, but even with that, there are work-arounds
Using events. This can work if you have the child swf dispatch a bubbling,
non-cancel-able event and have the event.target recorded by the
parent swf. You may have to adjust to avoid
SecuritySandboxViolations, however.
Using LocalConnections. The two detriments to LocalConnections are:
You will need to continually re-generate unique connection names to
avoid the error thrown by connecting two LocalConnections to the same
channel.
LocalConnections do have bandwidth limitations which can call
slowdowns if there is a high volume of traffic or if the messages are
too large.
Using direct manipulations like loader.content.foo.bar.baz;
I don't like this solution because it is much harder to maintain. It is
also much worse from a design perspective: you want to use
encapsulation as much as possible in this situation -- this technique
actively works against that.
So, I have an application where I'll be loading any number of swfs into a SWFLoader and removing them at runtime. The issue is - they are all timeline based movies authored in Flash CS3. I have very little control over what is in the movie other than the authors are not able to program any interactivity (i.e. no event listeners) but I want to make sure I am using the best technique to stop and trash these things so they don't hang around in memory too long. Here is the process I use right now to get rid of them:
Try stopping the content using MovieClip(content).stop();
Remove any listeners that I know about (ENTER_FRAME, etc...)
Set the source of the SWFLoader to null.
cross fingers, pray, make sacrifice (human if need be)
So that seems to work but is some better (or more comprehensive) method that you guys use to accomplish this same task? In all honesty - I'm not entirely sure these things aren't just kept in memory but I don't hear them so I don't know that they are there...
That's pretty much right. Loader (what SWFLoader uses under the hood), removes it's loaded clip from the stage with .unload(), but depending on what the clip has referenced the clip still runs and events still fire.
Flash Player 10 added .unloadAndStop(), but that is mostly just doing the same thing you are.
You could also try giving an explicit SecurityDomain (and probably ApplicationDomain so any contained classes are separate as well) in the optional LoaderContext parameter to Loader.load(), to try to prevent it adding event listeners outside (if you don't trust the clip not to), and avoid forming any references to anything from the clip so it can be garbage collected — but apparently it might still not unload in some cases.
In our application (a game), in some cases it can't run fast enough. Obviously we'd like to speed it up, but in the mean-time when this happens it causes many problems (or if it's not causing them, the two are related).
The one which is least related to our own functionality is that the built in Alert.show() method stops working. Typically the full-screen transparent box appears but not the actual popup. I believe this is down to Flex giving all available cycles to other tasks... but it's proving difficult to investigate analytically so I am happy to hear another explanation.
To clarify, core parts of Flex are simply not working in this situation. I've stepped through the code for instance where a new element is added to the screen, everything happens and the addChild() method is called on the main display canvas... but then the element does not appear. If we then disable our update loop, the element suddenly appears.
So whether Flex is supposed to run the exact same code or not, somehow it IS blocking is some strange way. As I said, even the Flex Alert.show() method doesn't work.
All Flash content is executed frame-by-frame - Flash executes one frame's worth of code, then updates the screen, and then waits until the next frame update.
When Flash can't keep up with the specified framerate, all that happens is that instead of waiting between frame updates, Flash does them as fast as it can with no waiting in between. So the only visible difference is that frame updates occur less frequently. There are never cases where code is skipped, events are dropped, or screen redraws are skipped for performance reasons (unless you've found new bugs).
So the most likely culprit is that either you have a problem with code that's very time-dependent (such as code that expects two timers to trigger on the same frame), or some other problem that's being misdiagnosed. (For example, maybe there's a bug causing a slowdown, rather than a slowdown causing your bug.)
I'm not too sure if Flex has some additional performance handling of it's own. But for pure actionscript the only thing that would happen is the framerate would slow to a crawl, everything will happen normally just slower. If you stack very large amounts of transparent or masked objects you might get some weird behavior, but that should be more noticable.
And I guess telling you that making a game in Flex isn't that much of a good idea (just because of the performance overhead the framework has) is a bit late ;)
I like to make games in FLEX 3 (actionscript3), its actually pretty handy solution when compared to Flash CS3: good debugging environment without hassle.
Of course it depends on the game style which one is better, if you need lot of graphics you may like Flash more, but Flex allows you to use external images, components, etc. Notice I am not talking about Flex XML project here.
Answer to your performance issue: You can use e.g. old MacOSX machine to see what happens in a very slow machine, a few solutions are:
- move objects more than x++ y++ pixels when machine is old
- reduce objects
you can detect with a timer how slow machine is..
I would like to make apps that can run in a flash enabled browser and decided flex would be best for me so I started to learn that. AFAIK flex has a GC but how that GC works compared to the ones in other languages I've seen may be a bit different. I have found this but apparently there is (or were) huge memory leaks that could not be prevented and a lot worse when run in IE.
Is flex safe to trust now. Have any leak that were there been fixed?
I work on a fairly large and complex Flex3 app, and we haven't had any terrible or uncontrollable memory leaks - even in IE.
This post by Grant Skinner on garbage collection in AS3 was very helpful to me. It was posted in 2006, but it's still entirely relevant (as we're still using AS3). It's part of a three-part series he did on resource management - there are links to the associated articles in his post.
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
Something to understand/remember is that Flex apps run in the Flash Player - maybe you're clear on that, but I don't think it's entirely obvious if you aren't coming from a Flash development background. At least, I don't think it would have been for me. :)
There is an Adobe DevNet article that was published describing Flash Player Garbage Collection:
http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
When you program in ActionScript your biggest concern related to memory is making sure that you do not keep references around to objects that are no longer needed. For example, when you add an event listener make sure that you also remove that event listener when it is no longer needed (or learn how to leverage weak references).
In my experience the Flex Framework is generally good at this type of memory bookkeeping. The memory footprint of your application will come down in part to how well you code with the garbage collector in mind.
If you do encounter issues with ever increasing memory, you can leverage the Profiler to take and compare memory snapshots in order to find leaking objects. You can find information about the Profiler here:
http://livedocs.adobe.com/flex/3/html/profiler_1.html
A couple resources you can have a look at are : Flex Bug Tracking which you could peruse for memory leak bugs that have been fixed/logged. Also in the Flex Builder IDE you can use the Profiler to monitor memory/CPU usage etc. I cant specifically answer your question. I think that part of the responsibility here is on you, the developer, and how you have designed and managed your code. Apart from that, I have noticed using the Flex 3 framework (or ASVM2 in general) that it has done a good job of managing the GC for me. Keep in mind as well that Flex 4 is around the corner.
In my experience, Flex 2 will leak significant memory over time, especially in Firefox 2. I'm not sure about Flex 3.
People at my job say there is a bug with ComboBox and autocomplete Event.