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

后端 未结 4 1916
天命终不由人
天命终不由人 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:31

    Yes - the behaviour is as you describe. The general rule for garbage collection is that objects only become eligible when you cannot reach them through any hard reference. Any objects you can reach this way will thus never be garbage collected (with the exception of soft / weak references.)

提交回复
热议问题