File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About gc

 
Mini Pilla
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are gc???
Vector v1 = new Vector()
Vector v2 = new Vector()
v1 = null;
Vector v3 = v1;
v1 = v2;
I think it is 1.
Plz share some views.
Thanx
Rajani
 
tvs sundaram
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you set an object reference to null, then the object pointed by that reference is eligible for GC. At the end of the program you cannot tell how many objects are garbage collected (As such Garbage collection algoritm is specific to particular VM and we cannot comment on that )but you can say how many are eligible for Garbege collection.

Coming back to your question I would say that one object is eligible for garbage collection after the line you have set the corresponding reference to null.
HTH
Any comments... Welcome
tvs
 
Bartholemu Smith
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the question is how many are gc'd the answer is zero. You cant force garbage collection. If the question was how many are eligible then thats a different story. I would say 1..which happens after line 3.
Hope that makes sense
Faisal
 
Mini Pilla
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya, I meant how many are eligible for GC.
Thanx for the sharing.
Rajani
 
r balasubramanian
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel, since all the objects are null (you have only created an object using new operator and nothing has been referenced), all the three objects are eligible for gc. Correct me if I am wrong.
Balasubramanian
 
tvs sundaram
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r bala,
u have made me think more.
But i am not able to answer it straightaway. I think u have a valid point for discussion.
Uninitialized objects have default value null. Does it mean they are eleigible for GC?
Ranchers, pls help......
tvs
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi TVS,
Yes!As long as you donot initialize the Object the reference is pointing to null, and hence is eligible for Garbage Collection
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
One object is eligible for gc.

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
tvs sundaram
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Jane,
Your reply is in line with my first post on the same thread.
But think about Bala's question and Sandeep's answer.
Do you mean to say that the default initialization to null for objects has nothing to do with GCing..
May be you can add a few lines of explnation..

However your diagrams were pretty good.

tvs
 
r balasubramanian
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane
Your explanation is interesting. But, I feel when an object is not initialised its default value is null. Since the objects with a value of null is eligible for GC, all the objects in the examples are eligible for gc. If I am wrong please explain me with more details. Thanks
Balasubramanian
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would like to say, there is difference between object reference and object. When there is no more object reference linking to the objects, the objects are eligible for GC.
In our case, there are two objects and three object references. When the object reference <code>v1</code> is set to <code>null</code>, the first created object <code>new Vector()</code> has no more object reference pointing to it, so it is eligible for GC.
The statement <code>Vector v3 = v1;</code> doesn't create any object.
Correct me if I am wrong, hope it helps.
Guoqiao

Originally posted by r balasubramanian:
Hi Jane
Your explanation is interesting. But, I feel when an object is not initialised its default value is null. Since the objects with a value of null is eligible for GC, all the objects in the examples are eligible for gc. If I am wrong please explain me with more details. Thanks
Balasubramanian

 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Guoqiao is correct. 'v3' is only a reference variable. A reference variable contains the memory address of an object, not the object itself.
You can declare as many reference variables as you like and they can all be set to 'null' but that just means they don't point to any object in memory.
The 'new' operator must be used to create an actual object (the one exception is the String literal, in which case 'new' is implicit).
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As far as I understand, the reference has a pointer to an object in memory.
If you don't initialize a reference, i.e if it is not pointing to an object in memory, then it implicitly means that you are pointing to null type of literal.
I understand, when we mark the reference to null say in this case
<pre>
Vector v1 = new Vector()
v1 = null;
</pre>

we are marking the object {new Vector()} for garbage collection.This means the pointer to this object which is stored in v1 reference is removed and the memory held by this object is reclaimed.
However, the question is after this resource had been reclaimed (on execution of v1 = null), is there anything in the memory like reference v1?Will the GC would allow the uninitialized references to remain in the memory?I don't think so.I believe, v1 along with the object pointer (and the object, ofcourse!) would get removed from the memory by the GC.
Hence, when we donot initialize the variables, it means the reference will be removed from the memory at some point of time.The algorithm of the GC would decide when this would happen.
Would like to hear some comments on this!
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
tvs sundaram
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sandeep, Your quote
-------------------------------------------------------
However, the question is after this resource had been reclaimed (on execution of v1 = null), is there anything in the memory like reference v1?Will the GC would allow the uninitialized references to remain in the memory?I don't think so.I believe, v1 along with the object pointer (and the object, ofcourse!) would get removed from the memory by the GC.
----------------------------------------------------------
There was an intersting explanation by Jim Yingst in some thread on this topic. I donot remember the contents exactly but there are something like hidden or secret references by VM which is not accessible to the programmer which keeps track of this uninitialized references and null set references to GC them.
Hope my understanding is correct.
If wrong pls correct.
tvs sundaram
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic