• 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

another GC doubt

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from K&B



question: When // dostuff is reached, how many, objects are elegible for GC?
a-0
b-1
c-2
d-Compilatio fails
e-it is not possible to know
f-An exception is thrown at runtime.


So i thought like this:

c1=null; so 1 object

c2 = new Cardboard();
then c2 is passed as an argument, since is an object what is passed is a reference to the object c2 representes, since on the method



the object which is passed by reference is set to null so c2 no longer poits to the object that inicialy it pointed, but instead it points to null

so: c2=null, 2º object elegible for GC

since c3 is initialized with the object return by the go ethod and it returns null

so c3 is null, 3º object elegible for GC.




Now i know that my way of thinking isn't correct i just can't understand why!


 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So i thought like this:

c1=null; so 1 object


c1 is no longer referred to therefore its absolutely eligible for GC.


c2 = new Cardboard();
then c2 is passed as an argument, since is an object what is passed is a reference to the object c2 representes, since on the method

view plaincopy to clipboardprint?

1. Cardbboard go(Cardboard cb){
2. cb=null;
3. return cb;
4. }

Cardbboard go(Cardboard cb){ cb=null; return cb; }

the object which is passed by reference is set to null so c2 no longer poits to the object that inicialy it pointed, but instead it points to null

so: c2=null, 2º object elegible for GC


what you pass to the method is a copy of the reference .so now local variable cb refers to the same object as c2 which is passed as an argument. So cb = null makes local variable cb have null value but c2 still refers to the same object.


since c3 is initialized with the object return by the go ethod and it returns null

so c3 is null, 3º object elegible for GC.


no object was created in the first place to be eligible for GC.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one object that is C1 eligible for GC.
 
João Martinho
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this change would make my line of thought true, as for c2 became eligible for GC?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
João did you try to compile that program?? It will fail to compile. Local fields cannot be marked static...
 
João Martinho
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seams that i'm getting more and more confused by the minute.

All you have said make since, except the part where a reference that not been inicialized isn't created (c3=null).

so what i get is:

objects that are never inicialized (ex:c3=null) are never eligible for GC because they are never created.

You always pass a copy as an arg to a method, if is a primitive it's a copy of the value, if an object is a copy for the adress that the original object represents.
But as soons as you modified the state of the local variable (on the cases it is an object) the jvm creates another object of that type and assigns that new stat to the local copy only.
on my exemple
cb==c2 -> true on line 3
cb==c2 -> false after line 4

Is this correct.

As for my last question it was a stupid question but i'm trying to learn "so much" that is killing my brain.

I'm sorry
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct.
But I think the way you are reaching to that conclusion is a little shaky (I was stuck here for quite some time myself )
This is how I do it...
The moment I see a GC question I start drawing the stack and the heap on my rough sheet. Draw the objects on heap only when you see "new" Also, if the class has a reference to a different class object dont forget to draw that too. Finally, make all connections with arrow marks and cross all connections that are set to null. If the object cannot be accessed from the stack(that includes accessing object through reference of another object) it is ready for GC.

For the following question from K&B (you got the question wrong!)



Here is the link to the Diagram, (Please excuse my external link I didnt know how else to upload a diagram)
Diagram


Therefore, 2 objects are eligible for GC.

Hope I answered your question.
Thanks,
Surya

 
João Martinho
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you did and yo may have teached me a way to finally get it.
 
Surya Priyadarshini
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am glad I could help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic