Does variable = null set it for garbage collection

后端 未结 8 1492
走了就别回头了
走了就别回头了 2020-11-27 19:04

Help me settle a dispute with a coworker: Does setting a variable or collection to null in Java aid in garbage collection and reducing memory usage? If I have a long running

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 19:13

    Setting an object reference to null only makes it eligible for garbage collection. It does not necessarily free up the memory,which depends on when the garbage collector runs(which depends on JVM). When the garbage collector runs,it frees up the heap by deleting only the objects which are eligible for garbage collection.

提交回复
热议问题