File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
Hi All,

I have doubt regarding garbage Collection.

public static void main(String args[]){
X x=new X();
X x2=m1(x);
X x3=new X();

x=x2; //Line 1

docomplexStuff();
}

static X m1(X x1){
x1=new X();
return x1;
}

After completing Line 1, how many objects are eligible for garbage collection. My doubt is whether object first referred by x (which is passed as argument to a static method) will be eligible for garbage collection or not.


}
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
in my view
1 object is eligible
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
HI,
Yes,I Think the Object first referred by x would be eligible for GC, Since
There are no live references to it after Lne 1 is executed

And only 1 Object is eligible for GC


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
sarang bharambe
Ranch Hand

Joined: Apr 01, 2005
Posts: 40
yaa
I also think only one object is elligible for garbage collection.


regards,
sarang


SCJP 1.4 (86%)<br />SCWCD 1.4 (86%)<br />----------<br />If You dont succeed at first time,call it Version 1.0 !!!
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

There is only one object eligible for GC after the line1

And that is the object initially referenced by x..

Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
I don't think I agree that the first object created to which x is referring is eligible for garbage collection. Java is a pass by value language. This is not only true for primitives but also for references in that only a copy of the value of refences is passed to methods. So the original reference x will still refer to the original object created on the first line.

The object to which x2 is referring may be eligible for GC. An object is created in the method and copy of the value of its reference is passed to x2. At the end of the main method x2 starts referring to the first object leaving no references for the second object.

Please let me know what you think.

Thanks,
Fes
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Awesome question (although someone changed it around a bit )
[ October 10, 2005: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Gokul Somasundaram
Greenhorn

Joined: Jul 17, 2005
Posts: 20
Let us go line by line:

public static void main(String args[]){
X x=new X();//Object creation under reference x.
X x2=m1(x);//A copy of Reference x is passed to the method.

static X m1(X x1){
x1=new X();
return x1;
}


X x3=new X();

x=x2; //Line 1

docomplexStuff();
}
Gokul Somasundaram
Greenhorn

Joined: Jul 17, 2005
Posts: 20
Sorry the above message is a Type O.

What i intended to explain was after returning from the statis method, at the line 1, we have

x=x2;

So the object which x was initially referring to has no reference now and it is ready to be GCed.

Anything wrong in my statement?
Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
Hi All,

Thank you for the replies. I understood that we need not count the copy variables passed to a method (even though it is static) while searching for the reference variables that are having references to the object in GC process. This is because, those variables are part of stack and will not exist once the method is completed. Please correct me if I am wrong.

I have another doubt regarding garbage collection which I came across in one of the mock tests. How many objects are eligible for GC when the control returned from this method?

public void method{
for(int i=0;i<=10;i++){
String tmp=Integer.toString(i);
System.out.println(tmp);
}
}

I have given answer 11 because, String tmp is local to the for loop and 11 objects are created in the for loop. once the controller comes out of the method, tmp is out of scope and the string referenced by this would be unreachable which makes the object referred by tmp eligible for GC.

But the answer given in the mock test is 10, the explanation given is "There is one reference tmp for the last object created, which makes that object not eligible for GC".

Please explain how it counts the reference variable that is not existing. When will the object referenced by tmp will be eligible for GC?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi roja,
As far as I think 11 objects will be GC'ed Since tmp is local and its scope ends after method returns.
and once there are no references left for an object, then it is eligible for GC. Isnt this true?

How come the answer is 10 then
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
public void method{
for(int i=0;i<=10;i++){
String tmp=Integer.toString(i);//Here when the loop increments the previously assigned value is eligble for GC because the new value is assigned now. When i=11 the loop will exits without executing the loop statements so the 11th (i=10) value is not eligible for GC.
System.out.println(tmp);
}
}


Anything wrong please correct me.

Thanks
Sampath
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
When the control returns from the method 11 objects will be eligible for GC
Because, the variable String tmp is declared inside the for loop so when the control comes out of forloop the "tmp" doesnt exists ..


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
Ok, you are right. Then how come the answer is 10.
Please explain


Thanks
Sampath
puran singh
Greenhorn

Joined: Oct 10, 2005
Posts: 1
11 objects are garbage collected
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

I say it's a terrible question because it depends on knowledge of implementation of the Integer.toString(int i) method. GC is about letting the VM manage memory, not about whether or not references are or are not internally maintained by some class.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Well Said. I agree with you Steve, we have to consider "Integer.toString()" also.
Since it a built-in method in the API we tend to skip it.
[ October 13, 2005: Message edited by: Srinivasa Raghavan ]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by Steve Morrow:
I say it's a terrible question because it depends on knowledge of implementation of the Integer.toString(int i) method. GC is about letting the VM manage memory, not about whether or not references are or are not internally maintained by some class.


Hi steve,
Can you please explain how does it affects the number of objects created(or GC'ed)if we do consider Integer.toString(int i);
As far as i think our code will still Provide GC with 11 objects to collect.
and the question for GC will always be asked in context to the given code.
Otherwise there are many method that create Objects at background and let them be GC'ed, so should we always count those objects as well, while answering the GC questoins. I dont think it is possible.

I am sorry if I have asked something silly. :roll:
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Can you please explain how does it affects the number of objects created(or GC'ed)if we do consider Integer.toString(int i);

Sure.

Take the question at face value, especially in light of the fact that the SCJP tests you over the behavior of the API, not the underlying implementation of core methods. You shouldn't know (or even be concerned with) how Integer.toString() is implemented. Perhaps it creates a new String; perhaps it doesn't. Maybe it keeps references to String constants for common conversions, like "0" or "1". Maybe it doesn't. The point is that the client doesn't know or care; they just know that when they call Integer.toString(1), it will return a reference to a String object. There's no way to know whether or not that String reference is retained internally or not and therefore whether or not that String is eligible for GC.

Does that help?

As far as i think our code will still Provide GC with 11 objects to collect. and the question for GC will always be asked in context to the given code. Otherwise there are many method that create Objects at background and let them be GC'ed, so should we always count those objects as well, while answering the GC questoins.

I dont think it is possible.
You're right; it's not possible. Well, it *is*, but it's not practical, and certainly not something worth testing. Similarly, you won't be asked those types of questions about GC in relation to factory-type methods like. There will be a clear, unambiguous answer to the question on the test, at least to those who have an understanding of basic concepts of GC in Java.

I am sorry if I have asked something silly.

Your question isn't silly at all. I hope I've been of some help.

Regards...
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One other quick suggestion...
11 objects will be GC'ed

Be very careful about your wording and your understanding of this, as this has a high probability of coming up on the test: objects will become *eligible* for GC. You *cannot* know whether or not an object *will* be GC'd. There is no guarantee that GC will run at any given time, so be very careful how you answer these questions...

Good luck!
[ October 13, 2005: Message edited by: Steve Morrow ]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi Steve,
Thanx For Correcting, and also for that explination.
it was surely helpful
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
One more small doubt about GC.
When GC runs, does it collects all the objects eligible for collection, or just enough to free the memory.
Someone asked this question and I answered that it collects all the eligibile objects.
Am I wrong?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

When GC runs, does it collects all the objects eligible for collection, or just enough to free the memory.

It's not specified, and therefore up to the VM implementation which garbage collection algorithm to use (there are several to choose from).

The Truth About Garbage Collection
Reference Objects and Garbage Collection
Garbage collection and performance

Hope this helps.
 
GeeCON Prague 2014
 
subject: Garbage Collection