• 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: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Muhgal and Rassmussen's Mock Exam

The answer given was c. I was thinking that since the object created in line 0 was a string literal, then it wouldn't be a candidate for garbage collection. Can somebody clarify this ?
[ Jess adjusted the line breaks in the CODE block... ]
[ February 03, 2003: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maria,
You are correct. String literals are not garbage collected.
On the real exam you won't find any questions about garbage collecting String objects.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand the question states "assuming no compiler optimizations are done" so I guess that means that a,b,c,d should be treated not like Strings but like any other objects which makes c the correct answer.
But it's good to know that we won't find any questions about garbage collecting String objects on the real exam :-)
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Culache:
On the other hand the question states "assuming no compiler optimizations are done" so I guess that means that a,b,c,d should be treated not like Strings but like any other objects which makes c the correct answer.
But it's good to know that we won't find any questions about garbage collecting String objects on the real exam :-)


Dan,
Compiler optimizations can cause objects to be garbage collected much earlier than one might expect. For example, a compiler optimization might cause an object to be garbage collected at a point in the program where it is still referenced but is clearly not going to be used any further. If you place a print statement in an object's finalize method then you can see indications of unexpected--but entirely logical-- garbage collection.
It is very important to remember that the behavior of the garbage collection mechanism is not defined by the Java Language Specification and no gc optimization will appear on the exam. For that reason, you should not attempt to answer questions based on undocumented gc optimizations. Instead, just remember the familiar rules about when objects become eligible for garbage collections--objects that are out of scope, no remaining references to the object in live threads, etc.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably he is thinking about sentence reordering. This kind of compiler optimization could interfere in our apreciation of when objects are g.c.ed
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, got the idea now.
Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic