April 29, 2008

AIR/Flex: Memory optimisation

Filed under: Adobe,Development,Flex — Steve @ 4:56 PM

Moving to doing some optimisation within my AIR application now, using the Flex 3 Profiler. I was noticing some interesting results, primarily that removing redundant event listeners and display children doesn’t always result in memory being freed up back to the system. Not as bad as a memory leak, but not quite what I wanted. As a result I’ve just been reading up a bit on optimising memory and garbage collection in Flex/AIR. From what I can see, although garbage collection in AS3 is actually quite good, there are a few tips, tricks and caveats…

Weak-referencing
It’s possible to help garbage collection along a little by using weak references on events listeners:


myObject.addEventListener("eventName", handlerFunction, false, 0, true);
The last parameter being the weak reference flag. This saves having to manually remove the listener and means objects will be automatically removed by the garbage collector, but it can make debugging problems difficult because the collector won’t necessarily remove the object on its next pass. A solution to this is to force the collector to sweep and remove de-referenced objects.

Manual garbage collection
Sean Christmann (eBay Desktop) demonstrates a method whereby it’s possible to manually force the garbage collector to sweep and remove memory objects using

flash.system.System.gc();
flash.system.System.gc();
(yes you need to call it twice, once to flag objects, once to remove them). As Sean describes though, using weakly-referenced objects can make it harder to track problems:

Trust me when I say that its a lot easier to debug an application with memory leaks due to strong listeners, then it is to debug an app in which users report random failures because underneath the hood weakly referenced objects are getting accidentally destroyed when the GC kicks in.

Flash Player
Flash Player’s garbage collector may sometimes ‘swap out’ dereferenced-but-not-yet-destroyed objects for new ones under its normal garbage collection process, that is it will maintain a certain memory ceiling and not go above it, but some object types are harder to destroy than others because they get referenced into the player – these are generally the types of object that work asynchronously, Loaders being one example; see Flash’s Dirty Secret, and Timers are another. Memory paging can also get quite fragmented because Flash was never optimised for the kinds of application where it might be running for hours, days or even weeks as an AIR application in the System Tray.

AIR Windowing
I’d not really considered this before moving to AIR, but the windowing capabilities mean that managing objects and garbage collection across multiple windows should be handled with care, because it’s possible to lose the ability to reference objects when application windows can be closed, or the application is running in the Tray and therefore has no focus. Sean describes another caveat where any EnterFrame handlers (which you might be using to control the collector) must be re-referenced across multiple windows.

The Elastic Racetrack
Finally, although this isn’t a memory thing, a word about execution time in Flex/AIR; each ‘frame’ of execution in your application is tied to the elastic racetrack concept. Flex has to balance the load between AS code execution, rendering and event broadcast/handling around a single continuous loop of execution, with allocation within each cycle dependent on the load for each. The depth of the display tree, or the execution time of a method will affect what gets done when during a particular frame or ‘lap’.

April 21, 2008

Flash 9 webcam support in Linux

Filed under: Flash,Webcam — Steve @ 3:23 PM

This is a bit annoying – we’d planned to use a number of EEE PCs with our Flash-based media streaming applications for a new kiosk-based project, and after some spectacularly unsuccessful tests today with an EEE PC and a range of webcams (including the built-in one) it seems that the problem is with Flash itself – it only supports VideoForLinux (V4L) and not the current V4L Level 2. V4L was removed from the Linux kernel way back in 2006 so I was surprised to find that V4L L2 isn’t there in Flash Player 9.

Apart from the EEE PC, this also discounts an number of other potential devices like the N800/810 and we’ll have to look at Windows UMPCs instead (which is likely to severely impact the budget), or alternatively install Windows on the EEEs (which is a hassle).

I agree with Aral – there’s an explosion of ultra-mobile, Linux-based devices out there now with webcams that could be used to enable video and audio-based communications and blogging, but at the moment we can’t lever them in Flash, and that’s a shame.

April 15, 2008

Cairngen

Filed under: Adobe,AIR,Development,Flex — Steve @ 11:24 PM

Writing about web page http://code.google.com/p/cairngen/

I wish I’d come across Cairngen a bit sooner, having recently just set up two Cairngorm-based AIR projects via cutting and pasting a skeleton Cairngorm project structure from Rob’s earlier examples.

Cairngen will set up a Cairngorm project structure for you (using Ant), and much more besides. For example it’s capable of auto-generating what are termed ‘sequences’, which are sets of event-command-delegate templates (plus other alternative combinations) that create the FrontController, ModelLocator and VOs required, plus all the relevant mappings and basic classes. The core templates can also be modified so that classes contain whatever boilerplate text you need. A little downtime now learning how to use Cairngen should save me work in future, and if I set up a basic project script anyone else using Flex/AIR here can should be able to get projects up and running more quickly.

April 9, 2008

AIRTour London liveblog

Filed under: Adobe,AIR,Flex — Steve @ 10:22 AM

Writing about web page http://onair.adobe.com/schedule/cities/london.php

Just some live notes from the AIRTour London event today…

Andrew Shorten
Adobe – ‘disruptive technologies’
RIA platforms – AMP, Buzzword Connect, all built on Flex/AIR
Why AIR? – occasionally-connected opportunities, efficient development, richer interfaces.
Branding on desktop – clever clothing store that can match clothes to an image dragged

Kevin Hoyt – Building AIR apps with Javascript
Showing mapping app using Yahoo Maps API
Export map view as PNG by dragging – encodes PNG on the fly, drag map straight to desktop
Using notepad app to write an AIR app
Starts with bare HTML page
Point to XML application descriptor file – template comes with SDK
changed to point to HTML file
Running AIR from Terminal to test app – runs directly
Dreamweaver demo – extension for AIR available
Writing tags like input id=”foo” type=”button” value=”save”
then writes JS script that handles event, using DOM to grab object, then runs as AIR app
AIRIntrospector – similar to Firebug
Maps AIR namespace to JS, allows AIR APIs to be called: var stream = new air.FileStream(); direct in HTML.
Now showing Aptana as IDE fr AIR/JS dev – Aptana now supports AIR
Sandboxed environment to prevent scripting hacks, adds some limitations

Mike Downey – Lighthouse programme – looking for key apps to extend reach of AIR (e.g. EBay desktop) *
Kevin Hoyt – Integrating HTML, JS and AS*
Mixing SWFs using SWFObject – then drags an image from Webkit into his Flash app window, makes a table from the BMP that contains each px colour value(!)
Uses FileStream to re-render px values as image in other application window
Basically levering the WebKit engine in AIR to mix HTML, JS and Flash in the same desktop window and all can communicate with each other.

Serge Jespers
Deploying and updating AIR applications
Self-signed applications report identity as ‘unknown’ on AIR installer, when known ico turns green
Uses any signing mechanism, Thawte etc.
Export Release Build, import certificate
Install badge on Labs
CheckForUpdate() script, parses application descriptor file for current version, compares, prompts user to upgrade

Daniel Dura – AIR APIs
Network detection, File I/0, Window chrome
Multi-window, z-ordering, utility windows (no taskbar entry), Native menus, Drag/Drop from various sources, clipboard, System Tray, notifications, Signing, Icons
..and much more!
Windowing control is very rich
File Sync/Async APIs
SQLLite included
Lots of code on Daniel’s site
Can drag a URL into browser, browser opens
Network/Socket Monitor API – URLMonitor(URLRequest);
Dock notifications

Bah, Macbook batteries don’t last long…

Jeremy Baines – from concept to TechCrunch in 5 days
Developed Alert Thingy – AIR-based social network aggregator with alerts etc, in-application commenting on peoples blogs, actually some very useful features and a good example of using AIR.

Developing Secure AIR Applications (Oliver Goldman)
Imported/loaded content executes with the Loader sandbox – does not run with application privileges by default
Sandbox bridges – opt-in on both sides, arguments are pass-by-value, allow communications between loaded content and application
Can expose function calls, but can’t pass objects
Document.write(), eval() are prohibited to protect sandbox
Suggests only loading signed content – XMLSignatureValidator API available
Implementing update functionality makes urgent updates easy should you find a bug – make updating work first

Gah, batteries really gone now…that’s all for today – excellent day, ice-cold Coke, beer, games, short sessions, direct contact with Engineering team.

April 2, 2008

360degree video

Filed under: Flash — Steve @ 8:47 PM

Writing about web page http://www.cornflex.org/?p=1

Wish I could embed the full example here, but I’ve just been looking at these awesome examples by Quentin Lengelé of combining footage shot with a 360degree camera, a 3DSMax ASE sphere model and Papervision 3D to create a fully 3-dimensional video experience (the original footage is by Immersive Media, who now also offer the same kind of thing commercially in a Flash-based player, lots of other demos on the link). Wish I could come up with this kind of stuff…

Papervision Video Screen

Merapi: AIR and Java

Filed under: AIR,Development,Flex — Steve @ 9:42 AM

Writing about web page http://adamflater.blogspot.com/2008/02/meet-merapi.html

I was wondering what the status of the Artemis Project was recently and had meant to follow it up, but a post on FlexCoders today highlighted Merapi, a new project that takes over from Artemis.

Merapi will provide a framework that uses Java to give AIR access to the system shell (and therefore have access to things like printing, scanning, hardware etc.). It’s very early days for the project but in a few months time it may be possible to create desktop applications using Merapi that do really useful things, without going near Swing. One to watch…

April 1, 2008

Cairngorm: Event Chaining with SequenceCommand

Filed under: AIR,Cairngorm,Development,Flex — Steve @ 8:36 PM

Writing about web page http://www.cairngormdocs.org/docs/cairngorm_2_1/com/adobe/cairngorm/commands/SequenceCommand.html

I was talking to Sara earlier about this and said I’d write something about it briefly. Sometimes when using Cairngorm you find that you need to chain a number of events. An example might be when an application initialises and you need to chain a number of events/commands that perform a number of sequential tasks. Typically what happens is that you end up following the event/command model and fire the chain by sending an event from a command, then sending another from the target command, and so on. However, Cairngorm includes a class that can help; SequenceCommand:

By extending SequenceCommand, you can specify the event that should be broadcast to the controller (causing another command execution without a further user-gesture) when the current command has completed execution.

This is really handy, particularly for those runtime initialisation tasks, and it doesn’t break the standard Cairngorm model of Events firing Commands because you define a nextEvent property that triggers another Cairngorm Event (which triggers the next Command, and so on). David Tucker’s excellent guide to Cairngorm goes into more detail and has an example. There are also many examples of people extending the SequenceCommand concept to perform other tasks.