When does an Object becomes eligible for Garbage Collection in Java ?
When does an Object becomes eligible for Garbage Collection in Java ?
An object becomes eligible for garbage collection in Java when it is no longer reachable by any live references from the executing code. This can occur in several scenarios:
Nullifying a Reference: If the reference to an object is explicitly set to null
, the object becomes unreachable unless it is referenced from another live path in the application[1][3][4][6].
Reassigning a Reference: When a reference variable is assigned a new object, the previous object that was referenced by this variable becomes unreachable if no other references to it exist[1][4][6].
Object Created Inside a Method: Objects created inside a method are eligible for garbage collection after the method's execution if they are not returned or stored in references outside of the method[1][4][7].
Anonymous Objects: Objects without any explicit reference (anonymous objects) are immediately eligible for garbage collection once they are no longer in use, typically after the execution of the line of code in which they were created if no references to them are stored[1][4][6].
Island of Isolation: Objects that reference each other but are c...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào