Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
Ankit Chandrawat
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 5..!!
 
marc weber
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?
 
Vishal Singh
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
 
Vijay Raj
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.
 
Bert Bates
author
Sheriff
Posts: 8945
17
  • 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....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!