If an object reference is set to null , will the Garbage Collector immediately free the memory held by that object?
If an object reference is set to null , will the Garbage Collector immediately free the memory held by that object?
No, setting an object reference to null does not immediately free the memory held by that object in Java. The garbage collector (GC) in Java operates independently of such assignments and does not immediately reclaim memory when a reference is set to null. Instead, the garbage collector runs periodically and determines which objects are no longer reachable through any active references. Only then does it reclaim the memory used by these unreachable objects[1][2][4][5].
Setting an object reference to null simply removes one reference to the object, potentially making it eligible for garbage collection if no other references to it exist. However, the actual process of memory reclamation is managed by the garbage collector's schedule and algorithms, not by the act of setting a reference to null[1][2][4][5]. The garbage collector uses a form of mark-and-sweep algorithm to identify objects that are no longer reachable from any active part of the application. Once an object is identified as unreachable, it is...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào