Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Garbage Collection

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone verify the answer of this code:
class Test {
static boolean status;

public static void Process(boolean status) {
if(!status) {
String s = "Java";
int i;
s = "JavaScript";
String x = "Java";
String y = x;
x=null;
i=1;
};
}

public static void main(String [] args) {
Test t = new Test();
Process(status);
}
}
How many objects are eligible for Garbage Collection?
1)Cannot determine.
2)1
3)2
4)3
5)compile-time error occurs.
6)No object is eligible for GC.
I feel it should be option 6 but the place from where i got this question says it should be option 2
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option 2 is correct, here is why...
1) 's' is assigned to "Java".
2) 's' is re-assigned to "JavaScript", making "Java" EGC.
3) 'x' is assigned to "Java" (it is not the same "Java" that was created on 1).
4) 'y' is assigned to what 'x' is referencing (in this case "Java").
5) 'x' looses its reference, however Object "Java" is still referenced by 'y', therefore it is NOT EGC.

hope this helps.
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sneha sn:
Can someone verify the answer of this code:
class Test {
static boolean status;

public static void Process(boolean status) {
if(!status) {
String s = "Java";
int i;
s = "JavaScript";
String x = "Java";
String y = x;
x=null;
i=1;
};
}

public static void main(String [] args) {
Test t = new Test();
Process(status);
}
}
How many objects are eligible for Garbage Collection?
1)Cannot determine.
2)1
3)2
4)3
5)compile-time error occurs.
6)No object is eligible for GC.
I feel it should be option 6 but the place from where i got this question says it should be option 2

 
Mishra Anshu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Mishra:
[QB][/QB]


Any object which has lost its reference is elgible for GC.
And it is very clear that the first "Hello" object is the only one
which has lost its EVERY reference.
So, it is quite clear.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you answer if the question didn't specify at which point in the program should we give our assumption on states of objects?
Definitely, for local variables, after you escape the method and before that the answer would be very different
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we assume that intent of the question is find out number of objects eligible for GC after method returns, I believe answer is 1 or possibly zero. So which one is the correct answer. I think that it depends upon what happens if an string object in the string pool is eligible for GC but has not been GCed and code calls for creation of another identical string in the pool. Is jvm going to use same string or create a new one?
 
sneha sn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isnt it that String objects are kept in a buffer and used for compile time optimizations. If so then object "java" will never be GCed.Please clarify regarding GC of Strings.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Isnt it that String objects are kept in a buffer and used for compile time optimizations. If so then object "java" will never be GCed


yes, the strings are kept in the pool as long as they have a reference. when they loose there reference then they are eligible for GC.
[ September 24, 2003: Message edited by: Vicken Karaoghlanian ]
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Halt! This is the focus police!
The good news is, because of the confusion over the String constant pool, the exam will not ask when String objects become GC eligible. The exam will however, in very tricky ways, ask when other types of objects become eligible.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic