Garbage collection on tokens vs. plain objects

M

micah

A coworker and I stumbled across an interesting question, and I can't
find an answer to it anywhere. Specifically: Does assigning an
instance of an object to a token make it easier or harder to garbage
collect, or does it have no effect? I.e., the following lines in a
closure:

var foo = new Bar();

vs.

new Bar();


If you don't need to reference that instance of Bar again, does it
make any difference to the GC whether you assign it or not, in terms
of counting references? Does it automatically get a zero count next
time the GC runs?

Also, does that instance of Bar get locally scoped without the var
keyword, or does it end up as an orphan floating around in the global
scope? Not that that really matters, since it'd be pretty hard to have
a collision without a token... it's more just a point of curiosity.

So, any ideas?
 
J

Joost Diepenmaat

micah said:
A coworker and I stumbled across an interesting question, and I can't
find an answer to it anywhere. Specifically: Does assigning an
instance of an object to a token make it easier or harder to garbage
collect, or does it have no effect? I.e., the following lines in a
closure:

var foo = new Bar();

vs.

new Bar();


If you don't need to reference that instance of Bar again, does it
make any difference to the GC whether you assign it or not, in terms
of counting references? Does it automatically get a zero count next
time the GC runs?

As far as I know no popular Javascript implementation uses a reference
counting GC, so in general it should make no difference. In practice,
you should test and post the results :)
Also, does that instance of Bar get locally scoped without the var
keyword, or does it end up as an orphan floating around in the global
scope?

Global scope. Just like the var, if the var foo assignment isn't in a
function.
Not that that really matters, since it'd be pretty hard to have
a collision without a token... it's more just a point of curiosity.

Please explain what you mean by "a collision without a token".
 
D

David Mark

A coworker and I stumbled across an interesting question, and I can't
find an answer to it anywhere. Specifically: Does assigning an
instance of an object to a token make it easier or harder to garbage

You mean assign a reference to an object to a variable.
collect, or does it have no effect? I.e., the following lines in a
closure:

var foo = new Bar();

The variable foo now references a newly created object.
vs.

new Bar();

To be safe, you should write that as:

(new Bar());

But what is the point? Unless the Bar constructor has some evil side
effects, this line does nothing (except allocate resources.) Nothing
references it, so it is immediately marked for GC.
If you don't need to reference that instance of Bar again, does it

In the latter case, you didn't reference it at all.
make any difference to the GC whether you assign it or not, in terms
of counting references? Does it automatically get a zero count next
time the GC runs?

THe first example will not be garbage collected as long as the closure
is referenced and foo still references the new Bar. To force it, you
could set foo = null or remove all external references to the closure.
Also, does that instance of Bar get locally scoped without the var
keyword, or does it end up as an orphan floating around in the global

If you omit var in the first example, then foo will be global. The
second example isn't part of any scope. Nothing references it.
scope? Not that that really matters, since it'd be pretty hard to have
a collision without a token... it's more just a point of curiosity.

So, any ideas?

If the newly created object is not referenced, then there is no way to
have a namespace collision.
 
T

Thomas 'PointedEars' Lahn

Joost said:
micah said:
A coworker and I stumbled across an interesting question, and I can't
find an answer to it anywhere. Specifically: Does assigning an
instance of an object to a token make it easier or harder to garbage
collect, or does it have no effect? I.e., the following lines in a
closure:

var foo = new Bar();

vs.

new Bar();


If you don't need to reference that instance of Bar again, does it
make any difference to the GC whether you assign it or not, in terms
of counting references? Does it automatically get a zero count next
time the GC runs?

As far as I know no popular Javascript implementation uses a reference
counting GC, so in general it should make no difference. [...]

Pardon? It should make a difference exactly because of that fact:
`foo' is a reference, no `foo' is not.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top