• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Garbage collection Q

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from 4test.com

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

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have read that-
"when a thread is dead,it can not be restarted but its methods can be called."
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic