Closures and garbage

June 10th, 2010 Victor Posted in beginner, intermediate 1 Comment »

Here is a transcript of a message that just landed in my Inbox via Tamarin-devel@mozilla.org

Tamarin has a hybrid storage manager; reference counting takes care of a lot of freeing, but there’s a regular garbage collector that will deal with cyclically referenced objects. Objects that can be reclaimed by reference counting are reclaimed relatively promptly; the rest may take a while. The back-up collector is driven by allocation volume, so as the program runs and allocates storage (and doesn’t free it via reference counting) then the collector moves forward to a point where it will have scanned the entire heap and will reclaim garbage.

The reference count increment you describe is legitimate; when you create a closure it captures (increments references on) all objects in its lexical scope, and in a regular class method ‘this’ is one of those objects. There are optimizations that Tamarin could be doing that it’s not currently doing, by not capturing objects that contains only names that are not referenced from the body of the function expression. In your artificial example that would have helped you out, but it’s less clear whether it would help you out in a real situation where the body of that function would not be empty but would probably be referencing something in the surrounding class instance (ie ‘this’).

(All this information provided to you with the proviso that I’m relatively ignorant about unloading in Flash :-)

–lars

I’ll try to (quickly and incompletely) point out one of the implications of what Lars explains here. This affects any ActionScript3 program that runs in Tamarin (including swf’s in Flash Player).

Let’s take a look at this code:

class MyClass
{
	public function myFunction():Function
	{
		var usedObject:Object = {};
		var unusedObject:Object = {};
 
		var myClosure = function():Object
		{
			usedObject.field = 100;
			return usedObject;
		}
 
		//...
		return myClosure;
	}
}
 
//Later on, we use the closure
var inst:MyClass = new MyClass;
var fn:Function = inst.myFunction();
var o:Object = fn();

Now, as we know, the function myClosure has access to all variables in it’s scope chain, including locals of myFunction and instance members of MyClass.
The question is, what happens with each of the variables when they are not needed anymore?
For example, we expect unusedObject to be garbage collected right away. Well, it turns out that this is not the case. As Lars Hansen explains here, all variables accessible by the closure are captured by the closure. So they don’t get deleted until the closure gets deleted. More than that, the instance of MyClass (the “this” object) is also captured (linked), although you can’t directly use the keword this inside myClosure (it would point to the global object).

Bottom line, careful with closures. While they provide great convenience in coding, they might come with serious performance penalty.

Let’s just hope this will come to reality in the near future: “There are optimizations that Tamarin could be doing that it’s not currently doing, by not capturing objects that contains only names that are not referenced from the body of the function [...]

Thank you Lars for these details!

AddThis Social Bookmark Button

AIR MiniBuilder on Linux screenshots

October 29th, 2009 Victor Posted in MiniBuilder, advanced, beginner, intermediate 11 Comments »

Quick visual tour of MiniBuilder ActionScript IDE running on Ubuntu Linux

IDE Overview
See ActionScript code completion for Player 10

MB Linux

“MiniBuilder is briefly inspecting your folders”
It takes a quick look in your foldes (user dir, up to a depth of 4) looking for Flex projects.
Run it, you’ll be amazed how many forgotten treasures will be unveiled :D

Brief

Build applications for Tamarin VM with MiniBuilder. The code completion provides only natives of shell Tamarin.
Check the console: you need to make the output file executable.
Linux fans, forgive the “.exe” ending, the output is not a windows exe! It is a projector – the Linux AVM packaged together with the abc data (compiled actionscript)

Tamarin AVM

These are just a few of the things…

Help this project, at least by showing your interest and it will grow!
Flash and AIR (especially 2.0) are providing quite some power to play with.

Check it yourself, 2.4M size, it’s tiny: MiniBuilder

AddThis Social Bookmark Button

Desktop Flash MiniBuilder is out!

October 28th, 2009 Victor Posted in MiniBuilder, advanced, beginner, intermediate 18 Comments »

Version alpha-1.0
Later Update: click below to install latest version.
Download count:

It works with Flex SDK 3.4 (+Java)
OS: Windows, Linux (Mac not yet)
See http://code.google.com/p/minibuilder/ for details

AddThis Social Bookmark Button

Flash MiniBuilder Part III – AsWing

July 22nd, 2009 Victor Posted in MiniBuilder, advanced, beginner, intermediate 11 Comments »

Flash MiniBuilder Part I
Flash MiniBuilder Part II
Flash MiniBuilder Part III

After a new run of fixes and add-ons, here is version 0.0.2

Most notable news:

  • line numbers
  • compiler errors markers in editor – tooltips show error message
  • minibuilder can now compile targeting player10 in strict mode
  • support for AsWing framework

Still missing:

  • hint list: it does not fit in the screen, does not cycle, can’s do pageup/pagedown, can’t doubleclick
  • completion: protected, internal. (the resolver currently deals only with private). there is a bug with completion at package level.
  • other bugs…

One more problem I noticed, the compilation is way slower online(4-6s) than the tests on my computer (0.5-1s). This will be fixed in next release.
Later Edit: issue fixed, abc extracter added to asc jar

Notes:
Compiling with asc is a bit different than with Flex. Asc is just a basic actionscript compiler, it can’t do a full dependency check. But you can use it to create quite complex programs, as long as you define your classes in the order of inheritance – base classes first, derived next. That’s right, you can write as many classes as you want in the same file. Unlike with Flex compiler, enclose each class in a package block. Just as you would concatenate a Flex ActionScript project in one big file. But no private classes.

And here is the playground:
Here is your chance to test the powerful AsWing framework right now. Check the reference and tutorials
(Download-able MiniBuilder web version 0.0.2 in a couple of days)

PS: Thank you iiley Chen for help and support
Thank you all for feedback.
Any help, feedback, bug report or feature request is welcome!

Stay put for new cool stuff, we are a step closer to the desktop version!

AddThis Social Bookmark Button