Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC doubt

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The answer is D. Can someone explain how? What happens when r=r+1 is done?
Thanks
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sun par:

I think it is obviously D.
A,B,C are wrong. S is local variable, so it will be collected when the method returns. So E is wrong as well.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Don. I somehow read the question as when r will be garbage collected. When will r be garbage collected?
Thanks
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think D too.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In response to the second question, the object referred to by 'r' will be eligible for collection after the statement r = null;
Not to be picky but the language is important - you can't ever say that an object will be garbage collected, only that it is eligible for garbage collection. The exam will try to trick you with phrases like 'when will the object be collected' and the answer is - you can never know.
But you can know when an object is eligible!
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the definition of 'eligible' is not just being null, it also including 'not refered again'. So I think here D is correct.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r and s are referring to two different String objects created from the String literal "abc".
At s = s + r; the original String object referred to by s is replaced by a new String object formed by concatenating the String objects referred to by s and r. The original String object referred to by s is then available for GC. So it is before statement labeled 4.
As for r, r = r + 1; The String object originally referred to by r is replaced by a new String object formed by concatentating String.valueOf(1) to the String("abc") object. So the original String object referenced by r becomes eligible before statement labeled 2.
Just my addition to the confusion
-Barry
Sorry for all the edits, the brain is acting up again. I do not always type what I mean anymore.
[ December 26, 2002: Message edited by: Barry Gaunt ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think GC applys to variable, not to string, int, float....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garbage collection refers to instances of classes, that is objects. We are talking about String objects referred to by variables.
-Barry
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, one of the conditions for GC is 'won't be refered again'. This makes sense to me.
In the example above, if r is GC'ed after statement 2: r=null, then what will happen when s=s+r come along.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r is NOT garbage collected, the String object that r originally referred to will be eligible for garbage collection; that is, an object String("abc"). r refers to a new object a String("abc1"), before it set to null.
Setting r to null then makes the String("abc1") object become eligible for garbage collection.
s = s + r, causes s to refer to a String("abcnull") and releases the String("abc") originally referred to by s to become eligible for garbage collection.
-Barry
Once again apologies for edits.
[ December 26, 2002: Message edited by: Barry Gaunt ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see your point. But I just wondering what will happen if after "abc" originally refered by r was garbage collected, we declare another variable:
f = new string("abc") ;
Wouldn't it be nice if we can reuse 'abc'?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does, sort of. The String literal "abc" is held in a JVM pool. The f = new String("abc") object is created from that pooled string literal. But it is a totally different object to other String("abc") objects as a test with == will show.
-Barry
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am tempted to ask another question:
What is exactly GC?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Memory to hold objects is stored inside the JVM in a data-structure called a Heap . If a situation arises where there is no longer a reference to an object's memory, then the memory can be reclaimed so that the memory can be utilized for storing other objects. This reclamation process is performed by a Garbage Collection algorithm running as a low priority thread inside the JVM. When the garbage collector thread runs you cannot, as a programmer, predict. What algorithm it uses you cannot, as a programmer, predict; it is implementation dependent. A programmer can only say that an object, after some point in the code, has no references and, in that case, is eligible for garbage collection when and if the garbage collection algoritm wants to do it.
For more I refer you to the literature. I retire to bed.
-Barry
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry, Don, Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic