• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects eligible for garbage collection

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we say, If an object refers to another object that is eligible for garbage collection, then the object refering is also eligible for garbage collection
i.e: A a=new A();
A b=new A()
b=a;
a=null; (a is eligible for garbage collection, then the object b refering to a is also eligible for garbage collection)

But with Strings,
String s="null";
String s1="not null";
s1=s;
s=null;(here s is eligible for garbage collection, then the object s1 refering to s will not be eligible for garbage collection as it still refers to "null")

Kindly suggest, as i am getting confused.
Thanks
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One clarificatin here is, 'a' is just reference variable to object A.
So, after the statement b=a the object refered by a is now refered by b.
So the object refered by a is not eligible for GC even you assign a to null because b is referring that.
But, here object referenced by b is eligible for GC because nothing is refer to this object now.

--
Venkata
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, after the statement b=a the object refered by a is now refered by b.


Agreed

So the object refered by a is not eligible for GC even you assign a to null because b is referring that.


As b is refering to the object refered by a. a=null, makes a eligible for gc.

But, here object referenced by b is eligible for GC because nothing is refer to this object now.


agreed.

Can someone clarify this.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can someone clarify this.



Clarify what? Are you agreeing with Venkata, that nothing is eligible for GC. Or do you still think that a reference can be GC'ed? I can't tell from your response.

[By nothing is eligible, I was talking about a=null, after both has already been assigned to the same object. I was not referring to the complete example.]

Henry
[ September 28, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 28, 2008: Message edited by: Arijit Daripa ]
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Object is eligible for Garbage collection, only when there are no references to it or it is in an Island of Isolation.

In the scenario described by you, the object is not eligible for GC since there is one reference to it even when the other refer.

And one more thing, you are setting reference to null and not the Object.
[ September 28, 2008: Message edited by: chander shivdasani ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Arijit & Chandar.

I got confused as i read this statement in Khalid's book

Ex question 8.1
If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.
Therefore, if an object obj2 is eligible for garbage collection and object obj1 contains a reference to it, then object obj1 must also be eligible for garbage collection.

Can you explain what the above statement means in terms of example.
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you agreeing with Venkata, that nothing is eligible for GC.



Henry,

I didn't say nothing is eligible for GC. Please look at my previous post again.

object referenced by b is eligible for GC because nothing is refer to this object now.



--
Venkata
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are getting confused between Object and its reference.

Consider this scenario

A--->Object1 ----->Object2

In this case, A is a reference, which is referring to Object1.and Object1 is referring to Object2.

Now, if A=null
Then there is no way you can reach Object1, and that means there is no way to reach Object2.

SO Object1 and Object2 both are eligible for Garbage Collection.

This happens in the cases,where a class has an Instance variable, which is reference to some other Object.


Now, read the statement given in K&B book with reference to this example
[ September 29, 2008: Message edited by: chander shivdasani ]
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But with Strings,
String s="null";
String s1="not null";
s1=s;
s=null;(here s is eligible for garbage collection, then the object s1 refering to s will not be eligible for garbage collection as it still refers to "null")



I dont think String objects are picked up by GC in any case.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vidhya,

quote:
--------------------------------------------------------------------------------
So, after the statement b=a the object refered by a is now refered by b.
--------------------------------------------------------------------------------


Agreed


quote:
--------------------------------------------------------------------------------
So the object refered by a is not eligible for GC even you assign a to null because b is referring that.
--------------------------------------------------------------------------------


As b is refering to the object refered by a. a=null, makes a eligible for gc.


quote:
--------------------------------------------------------------------------------
But, here object referenced by b is eligible for GC because nothing is refer to this object now.
--------------------------------------------------------------------------------


Not agreed.

if we use here b=null then this object which referenced by b is eligible for Gc.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Chandar and Vipin.

Chandar that explaining with ex made me feel so relieved of the confusion. I really appreciate that.

I also read that String object are never eligible for garbage collection. is that true in all case?

So if you have a String object within a method like in the below eg:
class A{
public static void main(String args[]){
A a=new A();
A b=new A();
a.test(b);
}
void test(A val){
val=new A();
String s=val.toString();// will String s be eligible for gc here after the method returns ?
}
}
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Objects are of two types.
One created on Heap and the Other created in String Constant pool.

for example,

String str = new String("Hello")

The above syntax creates 2 objects. One on Heap and other in pool.
Objects on heap are created only when you call new.

Objects in Constant pool are not subjected to GC until the class is unloaded by the JVM.

While objects on heap are subjected to GC.

I am not sure about the example given by you, but i think the object is created in String Constant pool. So it will be garbage collected only when the class is unloaded by the JVM.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah your right it create a String in the pool.
So in that case it will not be garbage collected until unloaded by JVM.

Thanks Chandar.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String Objects are of two types.
One created on Heap and the Other created in String Constant pool.



String objects are always created on heap.
Objects are never created in String Constant Pool.

String Constant Pool contains the reference to the objects on heap.
U can call this reference an internal reference.

But I think discussion on String Constant Pool is beyond the scope of SCJP.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bittoo garg:


String objects are always created on heap.
Objects are never created in String Constant Pool.



There are some Subtle differences between the following two statements

String str = "Hello";

String str = new String("Hello");

In both the cases, a String literal "Hello" is placed in the String Constant pool to provide reuse.It is because of the usage of String constant pool that String Objects are immutable.

The Strings Chapter in K&B book clearly explains this concept.
[ September 30, 2008: Message edited by: chander shivdasani ]
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chander,


The Strings Chapter in K&B book clearly explains this concept.


that is why i have written in my last post


But I think discussion on String Constant Pool is beyond the scope of SCJP



What I know is .... object is always there on heap in case of String literal. String Constant Pool has the internal reference to this object.
That is why these objects are not picked up by GC as reference is stored in String Constant Pool.

example:-
String str = "Hello";

in this case object is created on heap. But internal reference to this object is stored in Pool. This internal reference is then copied to String variable str.

This concept is beyond the scope of SCJP. So as far as SCPJ is concerned whatever you said is correct.

Please correct me if I am wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic