i have a confusion how gabage collector decides that an object is no more in use, do object have some scope?
like if i have code
class开发者_开发知识库 A { in x; m1(){}}
class B {A a=new a(); a.x=10; }
so i want to know that when the object become unusable
i mean in the above code if class be reaches to end line then when it exit that class do object a can go for garbage collection, and after that A class varibale will agian hold is default value of will permanently value 10
Only declarations (type, member, local) have scope. Nothing else.
Garbage collectors work by finding and tagging all objects that are reachable from known starting points (e.g., each thread's stack, all static variables, ...), and then blowing away objects that didn't get tagged. The full explanation is usually far more complex, but this is the essence of it.
Objects do have scope as any other variable and as defined by language rules.
An object is garbage collected when there are no other objects referencing it.
GC has its more or less complex algorithm for determining that . One of them is reference counting.
When a local variabla goes out of scope it looses a reference, if refernece coutn is 0 then is garbage collected.
Garbage collection is not deterministic, that is you can't decide eactly when to garbage collect it.
Setting a variable to null will basically make the variable garbage collectable.
精彩评论