• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on GC

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When is float object is eligible for GC??
1. public Object method(){
2.Float f = new Float("3.7f")
3.Object 0 = f;
4.f = null;
5. o = null;
6. return 0;
7. }
1.After line 3
2.After line 4
3.After line 5
4.After line 6
5.After line 7
I thought "After Line 4"(option 2)? But the given ans is "After line 5"?
Can some one explain me how???
Thanks,
Bala.

[This message has been edited by Bala Rajamani (edited October 12, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the answer is AFter line 5
Remember you have alias for the Float object
f and o
Just becoz f is made null doesnt mean that o is null too
So when o becomes null the object becomes eligible for GC
Ragu
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should know that an object is eligible for garbage collection when it has no live refrence & in your case all the refrence are null at line 5
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
there are 2 oblects pointing f and o to the float object.
when f=null ,o is still pointing when o =null,then there is no reference left for the float object and can be garbage collected
regards
neha
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code, there are two class variables pointing to the same objet i.e. Float f and Object o, so when f becomes null the obect is still referenced by o so that is why after line 5 is the right option since o becomes null too.
I hope it helps.
Jennifer.
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to clear Neha's miswording ---

...there are 2 objects pointing f and o to the float object.


There r 2 object references/variables f and o, not objects (Jennifer, i don't heard of class variables anyway ) pointing to 1 Float object. So after line 4 the object is still referrable/accessible by 1 reference o. After line 5 there r no object references left to point to the "Float object" (oobject instantiated from class Float), and hence becomes eligible 4 GC. Hope it helps.....
------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
[This message has been edited by Ashik uzzaman (edited October 14, 2001).]
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx ashik 4 correcting me.
These small things do matter
regards
neha
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bala Rajamani:
When is float object is eligible for GC??
1. public Object method(){
2.Float f = new Float("3.7f")
3.Object 0 = f;
4.f = null;
5. o = null;
6. return 0;
7. }
1.After line 3
2.After line 4
3.After line 5
4.After line 6
5.After line 7
I thought "After Line 4"(option 2)? But the given ans is "After line 5"?
Can some one explain me how???
Thanks,
Bala.
[This message has been edited by Bala Rajamani (edited October 12, 2001).]



Sorry for bringing back this closed item.
f -> 3.7f (at location 45000)
o = f
now o points to memory reference 45000, i.e,
o -> 3.7f
f = null
now, only o -> 3.7f and f is pointing to nothing and nothing else is referencing f
So, why can't f be garbage collected after line 4.
Any help is greatly appreciated. Thank you.
-Jay

[This message has been edited by Jay Kay (edited October 14, 2001).]
[This message has been edited by Jay Kay (edited October 14, 2001).]
[This message has been edited by Jay Kay (edited October 14, 2001).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,
The question is about the float object, that is the object you create with "new Float("3.7f")".
f and o are both references to this object. And the object can be garbage collected only when there are no references to it, that is when both f and o are null, which happens after line 5, not 4.
Hope this helps,
Radu
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Radu.
I still have doubts and the following can help me clear it.
If f was in memory location (say) 40000 and 3.7f was at memory
location 45000.
If we say o = f, will o point to memory location 40000 or 45000.
If o is pointing to memory location 40000, how will grab the
value of 3.7 at 45000
like in c programming, o = f makes o point to f, i.e, 40000 and
o = (*f) makes o point to 45000, i.e, 3.7f
How does this work in JAVA?
Thanks.

[This message has been edited by Jay Kay (edited October 15, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If f is at 40000 and the float object is at 45000 the content of f is 45000. Making o=f; copies 45000 to wherever o is.
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, making f = null will only make f point to nothing
o is not pointing to f. Then why would option 2 be wrong?
Thanks for your help.
-Jay
[This message has been edited by Jay Kay (edited October 15, 2001).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public Object method(){
2. float f = 1.23f;
3. float o = f;
4. f = null;
5. o = null;
6. return 0;
7. }
So in that case ,if i modify the code as shown above will it still be after line 5 or line 4.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In that case, making f = null will only make f point to nothing
o is not pointing to f. Then why would option 2 be wrong?
</BLOCKQUOTE>
o is not pointing to f, it is pointing to the object that f used to point to.

The object is what may get gc'd when nothing references it anymore. Not the reference may get gc'd when it no longer points to an object.

[This message has been edited by Marilyn deQueiroz (edited October 15, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harsha Jayan:
1. public Object method(){
2. float f = 1.23f;
3. float o = f;
4. f = null;
5. o = null;
6. return 0;
7. }
So in that case ,if i modify the code as shown above will it still be after line 5 or line 4.


You are not creating an object in this code, so you have no object to be gc'd.
 
reply
    Bookmark Topic Watch Topic
  • New Topic