• 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

Q of GC

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code, after line "doSomething", there are 2 objects of class A and class C eligible for GC, right?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think they are eligible for GC and first C need to be GC'ed before A.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claire,
I didn't understand your answer...
how many As and how many Cs do you think?
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arvind Varma:
i think they are eligible for GC and first C need to be GC'ed before A.


I suppose there is one object of class A and one object of class C for GC. But is there a sequence that one should be GC'ed first before the other?
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes!! you are right Meng..there is one C and one A to be GC'ed. They should be done in the sequence (First C followed by A) because class C will have a reference to C. SO C cannot be GC'ed until that reference is removed.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arvind Varma:
yes!! you are right Meng..there is one C and one A to be GC'ed. They should be done in the sequence (First C followed by A) because class C will have a reference to C. SO C cannot be GC'ed until that reference is removed.


I seriously doubt that this is the sequence in which garbage are collected. Perhaps if the GC is employing reference counting. But even so, it will just mark one object first before the other. But the actual reclamation of the object, I doubt that it follows the logic mentioined above.
Can someone verify this?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, one C and one A, AND Alton is correct. There is no way to know which of them will be collected first, in fact there is no guarantee that either of them will EVER be collected, and it's even possible that only one of them will be collected (although that last one is pretty unlikely).
For the exam, you only care about if and when objects become 'eligible' !!! The spec does not define when or how GC will work, and implementations may vary from JVM to JVM.
And really, in this case I'm not just being an exam hound , as a good Java programmer, you have to know what is left to the individual JVM, because you don't want to depend on the behavior of a particular JVM, it makes your code less portable! So forget all this nonsense about the GC sequence, relax, have a beer, and study the stuff you can do something about.
-Bert
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic