aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection Q" Watch "Garbage collection Q" New topic
Author

Garbage collection Q

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Question from 4test.com

In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:



"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"My question is, what happens in Java, if a method in an object is still processing some job.... "

You are referring to a thread. The general rule is that an object is eligible for gc if no live thread can access the object.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you Borderi,

So, in this case, if the process (line : e.calculatePay(); ) called before line no. 6 ( e = null is not finished, then object is not eligible for garbage collection?

As, method of the object is called from "main" thread. So main thread is still accessing that object and it will access that object (and method in that object) until method ends.

So, is it means ans. a is wrong in this case?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
"System.out.println" does not create a new Java thread.

The next line will not execute until the print line has been sent to the operating system, at which point any internal references to the object are gone.


Mike Gershman
SCJP 1.4, SCWCD in process
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
i have read that-
"when a thread is dead,it can not be restarted but its methods can be called."
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by prajkta patil:
i have read that-
"when a thread is dead,it can not be restarted but its methods can be called."


Is this a new question? (If so please start a new topic).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
As per my understanding:
When e=null is executed, there onwards there is no reference available to access the object initially referenced by e. so after execution of line 6 the oject is eligible for garbage collection.
Hence the answer is "Line 7 onwards"


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
Hi!

In this case, we have the variable 'e' set to null in line 7, but the same e points to a new object in line 8. i have read that a variable becomes eligible for garbage collection, only when the variable is no longer referenced in the program.

the scope of variable 'e' ends when the main method ends. so, e becomes eligible for garbage collection after line 10?

Plz correct me in my understanding.

Regards,
Jayashree.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi,
The ans a: line 7, is absolutely correct.
Dear JayaSiji Gopal,

As per your quote,
i have read that a variable becomes eligible for garbage collection, only when the variable is no longer referenced in the program.


You need to know there is clear distinction between a variable('object reference') and an object. e is only a reference variable that is pointing to an object of type "Employee". e can point to any object of Employee class as well as to any object of its subclasses. So e is not getting Garbage collected(to be specific eligible for GC) because it is just a reference variable, but it is the object created at the 3rd line of the program, that e was pointing. That object gets the eligibility for GC as soon as the reference varialbe 'e' is set to null, as 'e' was one and only reference to the object from the live thread(here main). So that object is eligible at the beginning of line 7 or in other words just after the processing of line 6 ends.
Now at line 7, e is pointing to a new object of type "Employee" and the old object created at 3rd line of the program that it was pointing has gone out of scope just after line 6 itself. Hence the ans a. Line 7.

Sorry for the detailed 'story' i only wished to clear JayaSiji Gopal confusion. Thank you.
Nikhil Kanjulli Menon.
SCJP 1.4.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi all,

To be precise the most perfect answer to this is " not possible to tell".
Optimally the ans a. line7 is acceptable provided nothing mysterious happens.

Explanation....

In this scenario we have two classes (1) Test (2) Employee
Now 'e' is a local variable. It is pointing to anobject created at 3rd line of the code. It is invoking 2 methods- (a)calculatePay(), (b)printDetails().
Since the implementation details of these methods are not revealed, it is not possible to say when or whether the object created at 3rd line be eligible for Garbage collection.

What if the Employee class is like this...

//code starts here

class Employee {
static public Employee eAlias;

public void calculatePay() {
// do neccessary codings..
// ...even here...
eAlias = this ;
}

public void printDetails() {
// do the needed codings relevant to your pgm
}

}
// code is over..for class Employee

In such a kind of implementation the object created at 3rd line which was initially pointed by 'e' is getting an alias which is 'eAlias'. This is still accessible for the live thread main. So that object will not be eligible for GC.

So the correct answer is obviously "not possible to say if the method implementation is not given." . If an option 'none of the above' is present, then choose that. Thank You.

Nikhil Kanjulli Menon.
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection Q