wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects eligible for garbage collection question" Watch "Objects eligible for garbage collection question" New topic
Author

Objects eligible for garbage collection question

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Hi,

It is good to be back (after finishing SCJP6! - BTW I got my certificate by mail yesterday - just after 6 days I took SCJP),
but still some of my silly questions linger

I would like to know what if any objects are eligible for garbage collection in the following code (this is my own made up code).



I am creating 10 anonymous objects that have no reference to them. After that I am going on an endless loop. Given that there are no reachable references to these objects, are they eligible for gc or is it not the case because the main thread goes on forever?

I guess my main question is about anonymous objects and their eligibility for garbage collection. Are these objects not considered eligible for gc if they are in scope i.e. it doesn't matter if there are no explicit references to these objects.

Thanks.
dipayan chatterjee
Ranch Hand

Joined: Oct 03, 2007
Posts: 47
Hi Larry

First of all congrats for clearing SCJP

I guess my main question is about anonymous objects and their eligibility for garbage collection. Are these objects not considered eligible for gc if they are in scope i.e. it doesn't matter if there are no explicit references to these objects


IMO garbage collection has nothing to do scope it basically is concerned whether an object on heap is acessible via a reference or not . in the for loop you are creating 10 objects on the heap that basically have no reference to them so even if the main thread is running can you so something like this System.Out.Println(?)

see here in place of ? you need a reference to an object to call its toString() but you dont have one do you?
so you basically cant acess any of the 10 objects so they are eligible for gc

secondly gc is something we cant control we can only request the JVM but JVM is the big boss.If at any point of time the JVM thinks that number of free memory locations are low it will carry out a gc operation it will not wait for your thread to fininsh .


Do correct me if i went wrong somewhere..

SCJP1.4 , gearing up for SCWCD 5
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Given that there are no reachable references to these objects, are they eligible for gc or is it not the case because the main thread goes on forever?


If you don't pass your object references to any other blocks, or method, etc, then you objects are eligible for garbage collection after that method or block. In your case, you even don't create a reference to those objects, so it's surely eligible for garbage collection after the for loop. But within the for loop, since there are no reference to those object to refer, so I think, those are eligible for GC!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Hey larry, the answer is "Yes" they are eligible sinece we have no way to reach them. But let us try to reason it,

Larry Olson wrote:Hi,

It is good to be back (after finishing SCJP6! - BTW I got my certificate by mail yesterday - just after 6 days I took SCJP),
but still some of my silly questions linger

I would like to know what if any objects are eligible for garbage collection in the following code (this is my own made up code).



I am creating 10 anonymous objects that have no reference to them. After that I am going on an endless loop. Given that there are no reachable references to these objects, are they eligible for gc or is it not the case because the main thread goes on forever?

I guess my main question is about anonymous objects and their eligibility for garbage collection. Are these objects not considered eligible for gc if they are in scope i.e. it doesn't matter if there are no explicit references to these objects.

Thanks.


Please note my conventions :-

X := integer Number
mem(X) = Memory taken by X objects
MEM = Memory allocated to JVM for our application to be executed.


Now suppose in Line1 (i have marked it), you created X objects such that

mem(X) > MEM , then your program would blow up during run-time and that should not happen as it is the ultimate aim of the Garbage Collection
to free up the memory whenever required by eating all the objects that are abandoned. So the fact is that as soon as there is no live reference to
an object it is availaible or i should say eligible for GC. But you cannot guarantee that iff it is eligible then thay are eaten up also.

But when JVM would go out of its limits then it is for sure that it would and it should run the GC thread.


SCJP 6.0 96%

(Connecting the Dots ....)
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
The rule is that, when there is no reference to an object on the heap from any live thread then that object is eligible for garbage collection.

In your program, there is only one live user thread, that is, main thread. And from this main thread there are no references to the objects those have been created in your program so all of these objects are eligible for garbage collection.

M.Sc. in CS, OCPJP6 93%
Pradeep Kr
Greenhorn

Joined: Feb 17, 2010
Posts: 22

As per my understanding, here created Object is eligible to GC as soon as it is created since we did not assign a reference to it. When JVM actually gonna collect these objects that is altogether different question. But as far as eligibility is concerned, as soon as Object created; it is eligible for GC even before next iteration of for loop.

Please correct me if I am wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for garbage collection question