Alex Rivera | Logout

Can you have memory leaks with a garbage collector?

Asked 2012-05-14T04:32:50.560
14

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 leak?

Considering a memory leak is allocations without any reference isn't that impossible or am I missing something?

Edit: So what I'm counting as a memory leak is allocations which you no longer have any reference to in the code. Large numbers of accumulating allocations which you still have references to aren't the leaks I'm considering here.

I'm also only talking about normal state of the art G.C., It's been a while but I know cases like cyclical references don't trip them up. I don't need a specific answer for any language, this is just coming from a conversation I was having with a friend. We were talking about Actionscript and Java but I don't care for answers specific to those.

Edit2: From the sounds of it, there doesn't seem to be any reason code can completely lose the ability to reference an allocation and not have a GC be able to pick it up, but I'm still waiting for more to weigh in.

Edit
Report

1 Answer

1

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.

answered 2012-05-14T04:42:14.120

Your Answer