• 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

doubt on garbage collection

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
12. void doStuff3() {
13. X x = new X();
14. X y = doStuff(x);
15. y = null;
16. x = null;
17. }
18. X doStuff(X mx) {
19. return doStuff2(mx);
20. }//here answer is not sure when object created at line 13 is garbage collected but outside the method we have made the references null so why is it not garbage collected?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object becomes eligible for Garbage Collections when its last live reference disappears.
1.The reference goes out of scope,permanently.
2.The reference is assigned another object.
3.The reference is explicitly set to null.

If you are declaring reference as null outside method ,it is not eligible for Garbage Collection because reference dies at end of method.

regards
SADASIVAKUMAR UTTI
SCJP1.4
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi archan,


can i have complete code.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Garbage control is under the conrol of JVM. The JVM decided when to run GC. So, though have made references NULL, its not necessarily that GC has been performed on them. Experinece suggest that it runs only when JVM senses that memory is running low.

Also, if you are writing one stand-alone code and running from the command prompt, do not expect to get the output as GC runs in other JVM than your program.


JUST FYI....

Among plenty of the garbase collector's algorithm, following are the two important once -
1. mark and sweep &
2. reference counting.
But Java specification does not gurantee any particular implementation. Its important to understand that any object becomes eligible for garbage collection when NO LIVE thread can access it. Object can be accessed by any thread only if object is reachable i.e. it should have some reachable reference.

Please write a finalize() method and invole GC.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post the missing code, please tell us where the mock question comes from. Thanks!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we're circling around the right answer
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is from kathy sierra gabage collection questions at the end of the chapter..
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, but I think we need to see the missing code in order to provide an answer.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------------
12. void doStuff3() {
13. X x = new X();
14. X y = doStuff(x);
15. y = null;
16. x = null;
17. }
18. X doStuff(X mx) {
19. return doStuff2(mx);
20. }//here answer is not sure when object created at line 13 is garbage collected but outside the method we have made the references null so why is it not garbage collected?
-------------------------------------------

Yes. The answer is "not sure when object created at line 13 is garbage collected" because we dont know about doStuff2() method. And refernce 'mx' is passed into that method. within that method that reference may or may not be retained. So answer is right.
 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are marking the object as null, you are recommending the GC to clear that. It is just a suggestion and you cant put any constraint on that
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic