How does Java Garbage Collection work with Circular References?

后端 未结 8 1711
旧时难觅i
旧时难觅i 2020-11-22 04:53

From my understanding, garbage collection in Java cleans up some objects if nothing else is \'pointing\' to that object.

My question is, what happens if we have some

相关标签:
8条回答
  • 2020-11-22 05:41

    Garbage collection doesn't usually mean "clean some object iff nothing else is 'pointing' to that object" (that's reference counting). Garbage collection roughly means finding objects that can't be reached from the program.

    So in your example, after a,b, and c go out of scope, they can be collected by the GC, since you can't access these objects anymore.

    0 讨论(0)
  • 2020-11-22 05:52

    A garbage collector starts from some "root" set of places that are always considered "reachable", such as the CPU registers, stack, and global variables. It works by finding any pointers in those areas, and recursively finding everything they point at. Once it's found all that, everything else is garbage.

    There are, of course, quite a few variations, mostly for the sake of speed. For example, most modern garbage collectors are "generational", meaning that they divide objects into generations, and as an object gets older, the garbage collector goes longer and longer between times that it tries to figure out whether that object is still valid or not -- it just starts to assume that if it has lived a long time, chances are pretty good that it'll continue to live even longer.

    Nonetheless, the basic idea remains the same: it's all based on starting from some root set of things that it takes for granted could still be used, and then chasing all the pointers to find what else could be in use.

    Interesting aside: may people are often surprised by the degree of similarity between this part of a garbage collector and code for marshaling objects for things like remote procedure calls. In each case, you're starting from some root set of objects, and chasing pointers to find all the other objects those refer to...

    0 讨论(0)
提交回复
热议问题