• 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??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following lines of code...

In the absence of compiler optimization, after which earliest line the object originally referred by a is definitely eligible to be garbage collected?
(Do not add spaces or dots. Just type in the number.)
Ans : 6
My question is in the line 6, when a is set to null, then it should be GCed.
but in the next line b is pointing towards "a". Then "a" cannot be GCed until and unless even "b" is set to null.
even in the next line a is being printed.
Garbage Collection is done only when the object is set to null and when there are no more references pointing towards that object.
Am I right?
Sonir
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
1. You mix up variables with the objects they reference to. These are two diferent things.
2. read the text of the question carefully.
It says:
which earliest line the object originally referred by "a".
Strings are immutable. In contrast to StringBuffer you can't change the value of as String variable without implicitly creating a new object (in the question compiler optimizations were ruled out.) So the variable "a" originally referred to a object to which no more references are pointing to, because variable a has been set to null in line 6.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does it mean "compiler optimizations are ruled out" ?
at line 6, the object ,a String object "Hello World" is GC 'ed. right ? this String object is destroyed ? right ?

Originally posted by Axel Janssen:
Hi Sonir,
1. You mix up variables with the objects they reference to. These are two diferent things.
2. read the text of the question carefully.
It says:
which earliest line the object originally referred by "a".
Strings are immutable. In contrast to StringBuffer you can't change the value of as String variable without implicitly creating a new object (in the question compiler optimizations were ruled out.) So the variable "a" originally referred to a object to which no more references are pointing to, because variable a has been set to null in line 6.

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark the object as line 6 becomes ELIGIBLE for GC. The Java Spec. does not guarantee that a dereferenced object will be GCed during the lifetime of a program, nor does the Java Spec. guarantee that a program will not run out of memory.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic