aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects eligible for garbage collection" Watch "Objects eligible for garbage collection" New topic
Author

Objects eligible for garbage collection

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
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

Joined: Aug 28, 2008
Posts: 148
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142

[ September 28, 2008: Message edited by: Arijit Daripa ]

SCJP 5
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

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 ]

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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

Joined: Sep 27, 2008
Posts: 55
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

Joined: Oct 09, 2007
Posts: 206

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 ]
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

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.
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
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.


Best Regards,<br />Vipin<br />MCA, SCJP5, SCWCD in progress
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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

Joined: Oct 09, 2007
Posts: 206

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

Joined: Aug 28, 2008
Posts: 148
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

Joined: Apr 29, 2008
Posts: 234

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

Joined: Oct 09, 2007
Posts: 206

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

Joined: Apr 29, 2008
Posts: 234
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for garbage collection
 
Similar Threads
Garbage collection
GC doubt
gc?
the "+" operator
String literals not eligible for GC?