Can you have memory leaks with a garbage collector?

前端 未结 4 1188
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 04:27

If I have a garbage collector that tracks every object allocated and deallocates them as soon as they no longer have usable references to them can you still have a memory le

4条回答
  •  Happy的楠姐
    2020-12-14 04:30

    Memory leaks don't just depend how efficient a garbage collection algorithm is, if your program holds on to object references which have long life time, say in an instance variable or static variable without being used, your program will have memory leaks.

    Reference count have a known problem of cyclic refernces meaning

    Object 1 refers to Object 2 and Object 2 refers to Object 1 
    

    but no one else refers to object 1 or Object 2, reference count algorithm will fail in this scenario.

    Since you are working on garbage collector itself, its worth reading about different implementation strategies.

提交回复
热议问题