java garbage collection -
i going through question on scjp preparation site . how answer correct?
what true objects referenced a, b, aa @ line labeled "// code goes here"?
class { private b b; public a() { this.b = new b(this); } } class b { private a; public b(a a) { this.a = a; } } public class test { public static void main(string args[]) { aa = new a(); aa = null; // code goes here } } a) objects referenced , b eligible garbage collection. b) none of these objects eligible garbage collection. c) object referenced "a" eligible garbage collection. d) object referenced "b" eligible garbage collection. e) object referenced "aa" eligible garbage collection.
answer: a
java doesn't use simple reference-counting garbage collector.
when jvm full gc run, walks entire object graph, marking each item finds. items aren't marked eligible cleanup.
since neither a
nor b
reachable main code anymore, won't marked , eligible cleanup.
Comments
Post a Comment