• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Garbage Collection  RSS feed

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



public class NewClass
{
static String enterTheDragon()
{
String a = new String("hello");
String b = new String("world !"); // 1
String c = new String(a + b + ""); // 2
String d = b; // 3
b = a; // 4
d = a; // 5
return c; // 6
}
public static void main(String args[])
{
String s = enterTheDragon(); // 7
System.out.println(s); // 8
}
}

When will the object on line 1 will be eligible for garbage collection.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 5..!!
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ankit Chandrawat:
... When will the object on line 1 will be eligible for garbage collection.


What do you think? And why?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Sunny,
object b ie. String b is still being referenced and returned at
line 6. So it wont be eligible for GC. Am i right?


Vishal
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String objects are immutable, right. So at Line # 2, we'll be getting a new string object. So, the string object will be eligible for GC.

But, Sunny, won't it be eligible for GC after Line # 4 itself?

Regards,
Vijay.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Let me put it into simple words:

at starting:
String a = new String("hello");




d = a; // 5
return c; // 6
"Hello" Object is created on the Heap..and a refers to it..!! a is reference variable, not an object..!!

String b = new String("world !"); // 1
"world" Object is created on the Heap..and b refers to it..!! so till now two object have been created..!!

String c = new String(a + b + ""); // 2
"Hello world" Object is created on the Heap and c refers to it..!! 3 Objects till now to which we can refer...!!

String d = b; // 3
Now one more REFERENCE VARIABLE Created..it also referes to "world" Object..so both 'b' and 'd' refers to same Object on the heap.

b = a; // 4

b reference is being changed, Now it refers to "Hello"..but still world Object is being referred by 'd'. so still not eligible for GC.

d = a;
d also refer to "Hello" Object, So now 'a','b','d' refers to "Hello" Object..!! c is referring to "Hello World"..!!
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Let me put it into simple words:

at starting:
String a = new String("hello");
"Hello" Object is created on the Heap..and a refers to it..!! a is reference variable, not an object..!!

String b = new String("world !"); // 1
"world" Object is created on the Heap..and b refers to it..!! so till now two object have been created..!!

String c = new String(a + b + ""); // 2
"Hello world" Object is created on the Heap and c refers to it..!! 3 Objects till now to which we can refer...!!

String d = b; // 3
Now one more REFERENCE VARIABLE Created..it also refers to "world" Object..so both 'b' and 'd' refers to same Object on the heap.

b = a; // 4

b reference is being changed, Now it refers to "Hello"..but still world Object is being referred by 'd'. so still not eligible for GC.

d = a;
d also refer to "Hello" Object, So now 'a','b','d' refers to "Hello" Object..!! c is referring to "Hello World"..!! NO ONE IS REFERRING TO "WORLD'..HENCE IT IS ELIGIBLE FOR GC.
 
author
Sheriff
Posts: 8981
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better to use objects of types other than String for GC questions. On the real exam you will NOT see GC questions that use objects of type String. The String constant pool makes it too messy.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Berts,

Even if we replace String by any other Object..!! That Object will be available for GC after line 5..!!!
 
Ankit Chandrawat
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!