memory safety and weak references

Hudson, Rick rick.hudson at
Mon Apr 1 15:12:39 PDT 2013

If the compiler can prove x does not escape the block and it is not used again then it is dead and the compiler is free to reuse the stack slot holding the last reference.

So I am arguing that x = null; is not required to kill x.

If we agree on that then I think we agree that someWeakRef.get(); is allowed to return null.

- Rick

-----Original Message-----
From: Brendan Eich [mailto:brendan at] 
Sent: Monday, April 01, 2013 5:56 PM
To: Hudson, Rick
Cc: Oliver Hunt; Marius Gundersen; es-discuss discussion
Subject: Re: memory safety and weak references

Hudson, Rick wrote:
> This brings up another interesting point. Do WeakRefs change a 
> compiler's liveness analysis?

Yes, of course.

> This could complicate some apparently useful optimizations.
> {
> var x = new Something();
> someWeakRef.set(x);
> // Is x dead? (yes) Is x required to contribute to the root set? (I 
> hope not.)

You dind't kill x yet. Did you forget

x = null;


> gc();
> someWeakRef.get() // null or foo?

If x = null; happened before gc() then null else the original ref.


> ...
> }

More information about the es-discuss mailing list