In the context of garbage collection, what happens when a reference data type is set to null?

  • The object is immediately removed from memory.
  • The object is marked for garbage collection but not removed.
  • Setting a reference to null has no impact on garbage collection.
  • Garbage collection is triggered, but it doesn't remove the object.
Setting a reference to null in Java means that the object that was previously referenced by that variable becomes eligible for garbage collection. It is not immediately removed from memory, but it is marked as a candidate for garbage collection. When the Java garbage collector runs, it identifies objects with no active references (i.e., references set to null) and reclaims their memory. So, while setting a reference to null doesn't immediately remove the object, it initiates the process of cleaning up unreferenced objects.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *