Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set references to null in an object to be GC'ed?

// in a garbage collected VM, destroy someObject:
someObject.a = null;
someObject.b = null;
someObject = null;

I have heard that in a good VM like Java's or C#'s, you shouldn't do this. Setting someObject's a and b to null will slow down garbage collection because the GC takes longer to find out that the objects a and b previously referred to are no longer referenced, while if you leave them intact the GC will immediately check upon them when cleaning up someObject.

Assuming what I heard is true (correct me if not), is it the same for AVM2, the ActionScript 3 VM (specifically in the latest versions of Flash Player)?

The reason I ask is I have a colleague who does it like that, because he has learned at a previous employer that it's faster, and that Flash has many quircks like that (which I find easy to believe).

I'm just wondering if that information is still up to date (for other Flash optimizations like that as well). In my experience optimization tricks like that get outdated rather quickly in a living platform.

like image 204
Bart van Heukelom Avatar asked Nov 06 '22 00:11

Bart van Heukelom


1 Answers

I can't provide you any specific, hard data on which is quicker - setting to null or not. I honestly don't believe anyone will.

The best I can do is provide you with some information around garbage collection, and you can make your decision from there.

From Flash Player 9 to Flash Player 10 Adobe has made some serious improvements, especially around memory mangement. There were several 'loading external swf's' memory bugs, unreferenced sounds sticking around, etc. Most of which have been rectified.

Grant Skinner has highlighted the techniques the GC use to establish what objects should be deleted. See this excellent presentation (with some cool interactions) on how it works http://gskinner.com/talks/resource-management/ (but do note the presentation is about FP9)

As he states there is Reference Counting and Mark Sweeping. Ultimately GC in AS3 comes down to references. If an object (a non primiative type) has a reference to it won't be deleted, but if it doesn't, i.e. there is no way to access it, it will be marked for deletion. BUT you cannot control when the deletion actually happens - might be this frame, might be the next.

Ultimately, 'null'ing' every variable/property (reference or otherwise), in my opinion is wasteful and very unmanegemable. Might be practical for smaller things, but when objects becoming larger with 100's of variables, you can't null all of them consistantly.

Down the track Flash Player will only optimise their GC for the right way of doing things, not the opposite.

like image 133
Chris Avatar answered Nov 09 '22 15:11

Chris