In the code below nameRef.get() is null , after name = null and System.gc().
nameRef.get()
name = null
System.gc()
import java.lang.ref.WeakReference; publ
Your demo has some problems. In fact, the size is 0 after System.gc(). In you situation is because when print the map's size GC has not finished yet, so the result is 1