Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

What's true about GC?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which are true?

A) You could override finalize() to create a more predictable garbage collection behavior

B) Invoking System.freeMemory() causes the JVM to perform a garbage collection process.

C) An object that has a valid reference cannot be garbage collected.

D) Objects created within inner classes can become eligible for garbage collection.

E) finalize() could create objects that become eligible for garbage collection

What's the answer and why?
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A is true.
The finalize method of Object class does nothing and returns normally. So, this method is generally overridden to perform clean up (usually).

B is false.
There is no way to actually force the GC to run. All we can do is make a request by invoking System.gc() or Runtime.gc(). When the garbage collection takes place is beyond our control.

C is false.
Consider the case, where two isolated objects have references to each other. However, no active thread can access them. In this case, these objects are still eligible for gc.

D, I believe, is also true.


Finally, finalize() can make objects eligible for gc ineligible by making active thread reference these objects.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Firas,

Here's my answers and explanations.

A) Incorrect : You can never predict when GC will run. Even finalize() method can never be invoked.

B) Incorrect : Stupid. No sense !

C) Incorrect : Even objects with a valid reference can be Garbage Collected.
Try this code :

D) Correct. You deal with instances created within inner classes in the same way you deal in outer classes.

E) Correct. You can call finalize() in the same way any other method of your class. So, you can create any objects and then make them eligible for GC by setting null.
[ May 31, 2006: Message edited by: Edisandro Bessa ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic