Forums Register Login

About gc

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
ya, I meant how many are eligible for GC.
Thanx for the sharing.
Rajani
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
Hi guys,
One object is eligible for gc.

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send

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 got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1036 times.
Similar Threads
JavaCaps Test 2, Question # 33
Guarenteeing G.C part
Garbage collector
Appending Vectors
Help please
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:14:56.