This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects eligible for garbage collection question

 
Larry Olson
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep Kr
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic