• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Mock Quesion on Garbage Collection

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Question 13: There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?
class Riddle {
public static void main(String[] args) {
String first, second;
String riddle;
if (args.length < 2)
return; a: first = new String(args[0]);
b: second = new String(args[1]);
c: riddle = "When is a " + first;
d: first = null;
e: riddle += " like a " + second + "?";
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
Select the one right answer.
1)d:
2)e:
3)h:
4)i:
5)j:
Lot of confusion about Garbage collection.....Can someone explain.
Thanks.
Mukti
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object is eligble for garbage collection as soon as there are no more references to that object. Since they are talking about the object that first is references, the String args[0], when will that object no longer have any references to it?
Take a look at it and tell me what you think.
Bill
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
If the reference to a object is set to null....the object is still there in the memory.And will be set to null when the value is set to null...as in this case...args[0] = null;
Sorry, for the ambiguous explanation.
Can you please let me know if, I am on the right track.
Anyway, Thanks
Mukti
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are no references anymore, then there is no way you can get to the object. So you are correct, once args[0] and the variable first is set to null, you can no longer see the object, so it is eligble for garbage collection. This one is a bit trickier because it is easy to think that once first is set to null after line "d" that the object would be eligble for garbage collection, but that is not true because you can still see the object by looking at args[0]. So it is not until after line "h" that the object can no longer be seen.
Bill
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What mock is this from?? Looks like one I'd like to take!!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to Java API
String(String value)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
Doesn't it means the new String(arg[0]) has created a new String object?
I think the answer is still d:
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mukti,
I think the correct answer is D.
The reason:
First of all an object is ready to be garbage collected once it is not referenced by any variable.
But here we are deling with Strings. Do u know the concept of STRING POOL.
A string pool is maintained by java which is checked whenever u create an object of type String. So if the value for which u r creating the String object exists in the String pool then a new object is not created instead the variable refers to the object which already exists.
e.g.
String str="hello";
String str1="hello";
here both these variable of string type refer to the same object in memory.
Here if I say
str1=null;
then the object "hello" is not available for garbage collection b'coz it is still referenced by str;
BUT YOU CAN CHANGE THIS BEHAVIOR USING new.
one u create a object using new then the String Pool is not checked and a new object is created.
e.g.
String str="hello";
String str1=new String("hello");
here both str and str1 store the same value but are seperate objects because 'new' forces the creation of new object.
PROBLEM ABOVE
in above problem first is created using 'new' so a seperate object is created for first and it is different from args[0] so when u say
first=null;
then the object is available for garbage collection.
So the correct answer is D.
This is my opinion.
In case I am wrong do inform me I shall be thankful
Neeraj Thakkar
[email protected]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
Dont you think at line d: first become null(Its handler become null), but at line c: riddle is still pointing to the same address of first. So though, we make first as null the object which has the id is still referenced by riddle at line c:
So till riddle points to a different object at line e: the string object created is not eligible for GC. I believe the correct answer is after line e:.
Correct me if I am wrong.
Thanks.
Nasser Aboobaker
[email protected]
[This message has been edited by Nasser Aboobaker (edited March 23, 2001).]
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I think d) is right, because
c: riddle = "When is a " + first;
does not point to the object first, but to a newly created String object.
Up to what I think its like riddle = new String ("When is a " + first);
correct me if I am wrong
Axel
 
Nasser Aboobaker
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
I believe all first is having an objects address. Let us say if Object is at memory address 15000, all first is having that address. So if the object at memory address 15000 is type of String and having a value of "Hello", first is pointing to that object.
When you say anotherfirst = first then it means anotherfirst is also pointing to the same address as first (15000). anotherfirst does not get a value of "Hello". All it gets is the copy of the address which is pointing to the object "Hello".
So when you say riddle = "another string" + first, actually we are saying riddle = "another string" + (the object at memory 15000)
So the string object "Hello" is referenced by riddle tooo. (That is what I believe)
If I am wrong please correct me.
Nasser
[This message has been edited by Nasser Aboobaker (edited March 23, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic