Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 1 how many objects are eligible for gc.



I think only line2 is eligible to be garbage collected.
Because in line 2 we are passing the object and in the domethod() a new object is created and returned so the one created in line 2 is lost and is eligible to be garbage collected.

Correct me if i am wrong.
Thanks in advance
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. There are a total of 4 objects created. The first of these is never assigned to any variable, and that's the one eligible for garbage collection. As of line 1, the other 3 objects remain referenced by variables d, c, and y.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks marc
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


At line 1 or the correct wordings are after the main method get completes , how many objects are eligible for GC ??

The correct answer is 4 .

Right ?

Thanks .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
...after the main method get completes , how many objects are eligible for GC ??

The correct answer is 4 .

Right ? ...

Right. The 3 variables referencing call objects are all local to main, so as soon as main returns, there's nothing referencing those objects, so they're all eligible for garbage collection.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks marc
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the original question:

After line 1 how many objects are eligible for gc.

code:


public class call
{
public static void main(String[]arg)
{
call d=domethod(new call());//line 2
call c=domethod(d);//line 3
call y = new call();//line 4
//line 1
}

public static call domethod(call a)
{
call b=new call();
return b;
}
}

Shouldn't the answer be 4, even though line 1 is inside the main method. If there are no future references to the object in the method, they could be garbage collected, though the method has not completed. Right?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Rama:
... If there are no future references to the object in the method, they could be garbage collected, though the method has not completed. Right?

The thing to consider is that these variables are still in scope. Although they're aren't being used after line 1, they are still available for use.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic