Question

If I have an scanner object and use the method close(), will it become elegible for garbage collector after that line? Or will it only be elegible if I write "scanner = null;"?

If I don't set an object to null (either a scanner or not) in any part of my program, may I say it will become elegible for garbage collector at the end of the program? Or will it be never elegible for garbage collector?

Was it helpful?

Solution

close() invokes implementation of close() method simply it has nothing todo with GC, object will be eligible for collection only when there is no live reference exist

OTHER TIPS

If you use local variable for scanner object inside of the method and not assign this variable to any other object with wider scope, then when you exit method containing this local variable the object is eligible to garbage collecting.

As many said invoking close() does not make object eligible for GC. By the way it may closes or de-references some internal objects, so they would become eligible for GC. By the way when handling IO invoking close() frees some OS handles (and expensive resources), which are usually more important than GCing a single object.

An Object becomes eligible for Garbage collection ,if its not reachable from any live threads or any static references in other words you can say that an object becomes eligible for garbage collection if its all references are null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top