• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Roja Rani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my view
1 object is eligible
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sarang bharambe
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaa
I also think only one object is elligible for garbage collection.


regards,
sarang
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome question (although someone changed it around a bit )
[ October 10, 2005: Message edited by: Bert Bates ]
 
Gokul Somasundaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you are right. Then how come the answer is 10.
Please explain


Thanks
Sampath
 
puran singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
11 objects are garbage collected
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,
Thanx For Correcting, and also for that explination.
it was surely helpful
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic