Does Java GC destroy objects if instance variables still have reference?

后端 未结 4 1917
天命终不由人
天命终不由人 2021-02-07 08:51

I\'ve read through some of the Java garbage collection guides online, but I\'m still a bit unclear and wanted to make sure so that I don\'t have memory leaks in my code.

4条回答
  •  梦如初夏
    2021-02-07 09:19

    Yes of course.

    Remember, what you store in the var field is actually reference to the object, not the object itself. So when GC collects obj1, the var object is untouched (must not be touched), especially since it has a reference from obj2, which is still alive and well..

提交回复
热议问题