The null assignment in the following snippet is removed when compiled with -optimise which leads to different behavior and can result in memory leaks. It seems to be a bit of an edge case, since obj is a local var, but it is surprising nonetheless. I actually encountered this in a unit test testing for leaks, where a setup like below can be common.
object WeakBug {
def main(args: Array[String]) {
var obj = new Object {}
val ref = new java.lang.ref.WeakReference(obj)
obj = null
System.gc()
//Thread.sleep(500)
assert(ref.get == null)
println(ref.get)
}
}