• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
Greg Torrance
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Question 65 in the Boone exam ...
Which statements about garbage collection are true?
Select all valid answers.
a) You can directly free the memory allocated by an object.
b) You can directly run the garbage collector whenever you want to.
c) The garbage collector informs your object when it is about to be garbage collected.
d) The garbage collector reclaims an object´┐Żs memory as soon as it becomes a candidate for garbage collection.
e) The garbage collector runs in low-memory situations.
The official answer is (b), (c), (e).
(c) and (e) make sense to me, but (b) seems questionable ...
Mughal and Rasmussen say "Java does provide facilities to invoke the garbage collection explicitly. The System.gc() method can be used to force garbage collection, and the System.runFinalization() method can be used to run the finalizers for objects eligible for garbage collection."
but, ... Brogden says "The programmer can suggest to the JVM that now is a good time to run garbage collection with one of the following method calls, but there is no guarantee that the JVM will do it: System.gc(); / Runtime.getRuntime().gc();"
also, ... in the JDK docs for System.gc() it says "Runs the garbage collector. Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse."
I'm confused - (news flash :-) ... Can someone help me out? Can you, are can't you run garbage collection whenever you want?
Thanks a lot!
Greg
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a gray area, really - it depends what we mean by "run" garbage collection. You can certainly run the System.gc() method any time you want, which is in a sense part of the garbage collection system. And according to the API, "when control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects." But "best effort" is never really defined - it's possible that the system couldn't find any memory to free up, or even possible that it's so busy with some other high-priority thread right now that its "best effort" is, well, nothing really.
So we're caught between two possible ways to read the question:
  1. Can you invoke (or call) garbage collection directly? Yes.
  2. Can you force the system to free up memory when you want it to? No.

  3. Which of these do you think Boone's version is closer to? Personally I think it's closer to the first one (and apparently so does Boone, from his answer), but I believe the general consensus here is that the second question is what they probably mean to ask. Or putting it another way, the second question is one that most everyone agrees is a major "theme" that you should know about garbage collection, which Sun will probably test you on, while the first one is generally seen as less important. So if the wording is ambiguous like it is here, go with the second interpretation - it's probably safer.
    For what it's worth, most everyone who's taken the test reports that actual Sun questions are almost always clearer than this - so despite all the space I've taken writing about it, I wouldn't worry about it too much.
 
Greg Torrance
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input Jim!
Greg
 
J. Scott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great garbage collection question. It has inspired a follow up question concerning the same topic:
Can garbage collection ensure that a program will not run out of memory while running?
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the option
e) The garbage collector runs in low-memory situations

correct? is'nt that completely implementation specific? someone could write a jvm that does'nt and it would still be a jvm, right?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!