Garbage collection

20 views
Skip to first unread message

Aaron

unread,
Oct 12, 2009, 1:35:44 AM10/12/09
to nixysa-users
I am having some issues with garbage collection in firefox and I'm
wondering if anyone else has noticed the same issue.

The following code leads to cleanup of my nixysa exposed class:

function a() {
var plugin = document.getElementById("plugin");
var nc = plugin.NetConnection();
delete nc;
}

The following does not:

function a() {
var plugin = document.getElementById("plugin");
var nc = plugin.NetConnection();
if(nc.connected)
alert(1);
delete nc;
}

I have had a little search for reference counting problems with
NPN_Retain and NPN_Release but I'm not super familiar with what takes
ownership of what when it comes to NPAPI. I thought this might also be
a firefox bug. Can anyone shed any light on this for me? Documentation
seems scarce on this.

- Aaron

Antoine Labour

unread,
Oct 12, 2009, 11:54:52 PM10/12/09
to nixysa...@googlegroups.com
Lifetime issue with JS + NPAPI amounts a bit to black magic, since it mixes garbage collection and reference-counting.
The object can only be deleted if both of these are true:
- Javascript doesn't have any more reference to it and garbage-collects it
- The NPAPI ref count falls to 0.

Note 2 things however:
- I don't think your 'delete nc' line does anything. 'delete' is used to remove properties from objects, not to force-delete objects. If anything, it would delete the *reference* (ie the remove the variable nc from the scope), not the object itself.
- the Javascript interpreter doesn't have to run the garbage collector in a timely fashion, which means the objects may not be destroyed immediately.

In a nutshell, you unfortunately don't have any way to ensure that an object is destroyed. We worked around the problem in o3d by making the Javascript references "weak", and have a separate mechanism ("packs") to ensure lifetime, so that we could release system resources (e.g. GPU memory) in a timely fashion.

Antoine

Aaron Drew

unread,
Oct 14, 2009, 2:13:22 AM10/14/09
to nixysa...@googlegroups.com
Lifetime issue with JS + NPAPI amounts a bit to black magic, since it mixes garbage collection and reference-counting.
The object can only be deleted if both of these are true:
- Javascript doesn't have any more reference to it and garbage-collects it
- The NPAPI ref count falls to 0.

I figured us much but hoped for some better news. If the NPAPI reference counted variable was treated the same as a javascript variable inside the browser, when the ref count hits zero the variable reference could be removed and the garbage collection would eventually do its thing. As you say, it doesn't seem to work that way though unfortunately.
 
Note 2 things however:
- I don't think your 'delete nc' line does anything. 'delete' is used to remove properties from objects, not to force-delete objects. If anything, it would delete the *reference* (ie the remove the variable nc from the scope), not the object itself.
- the Javascript interpreter doesn't have to run the garbage collector in a timely fashion, which means the objects may not be destroyed immediately.

Yep. The delete was just there to make it crystal clear to the JS interpreter that the variable was not needed. I was initially thinking it might be an issue with scoping. I found the firefox GC kicks in between 10 and 60 seconds after my code ran. The main issue I had was that lost references to connection objects would remain connected. If it can't be helped I'll just have to make a note for users to remember to explicitly close() their connections. 

Thanks for your help! 

- Aaron
Reply all
Reply to author
Forward
0 new messages