I have a do-while loop like this
do {
SomeObject someObject = new SomeObject();
} while(some condition is met);
I wish to know what is the life-cycle of someObject object and when will they become eligible for GC collection.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Any object becomes eligible for garbage collection when it is no longer reachable by any reference that is currently in scope.
In your example, the instance assigned to
someObjectwill become eligible for GC at the end of the single loop iteration in which it was created, because you have only a single reference to it, and that reference goes out of scope at the end of the loop block.However, this assumes that you are not passing references to your object elsewhere during the execution of the constructor and there is no other code that passes references to your object from your loop.