Help coderanch get a
new server
by contributing to the fundraiser
  • 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

garbage collection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
in the following code
1. class myclass
2.{
3. public static void main(String [] args){
4. object c1 = new object();
5. object c2 = new object();
6. c2=c1
7. c1=null;
8. c2=null
9.
}}

where can gc be ivoked at the earliest? is it after line 7 or 8 ?i feel after line 7 but answer given is 8
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The strict answer to the question is that the garbage collector could be invoked at any time by the JVM.
I think that the question should be: What is the first line after which an object created in main is eligible for garbage collection? (I'm also assuming that object is a valid class defined elsewhere.)
The answer I would give is "After line 6", because after line 6, the object created in line 5 is no longer referenced.
However, the object created in line 4 will only be eligible for garbage collection after line 8, when both variables no longer refer to it.
[ February 24, 2003: Message edited by: John Paverd ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"arunabhanu"-
Welcome to the JavaRanch!
We like to keep a nice professional lookin' image here (despite that one eyed moose)... so please adjust your displayed name to match the JavaRanch Naming Policy. You can change it here.
Thanks! and again, welcome to the JavaRanch!
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic