This is a question from a mock exam which statements about garbage collection is true? a) you can directly free the memory allocated by an object b)you can direclty 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. d) the garbage collector runs in low memory situations my answer is a) this is false bcoz you cant directly free memory allocated b) true bcoz you can run the Runtime.gc() and System.gc(); c) true //not sure d) true e) false Im not sure about my answers bcoz no answer was provided in the mock exam. Am I right? regds, joy
[This message has been edited by Jim Yingst (edited May 22, 2000).]
Joy, a) false b) false. You call System.gc() or Runtime.getRuntime().gc() to "suggest" to the garbage collector to reclaim memory used by unreferenced object. You cannot "run the garbage collector". c) true. The finalizer method inherited from Object is used for this purpose. You have to override this method to free resources other than memory. If you don't override this method, the default one in Object is executed (the default finalizer does nothing). d) false. As b) shows that you cannot determine when the garbage collector is run, you cannot tell when is the memory garbage collected. Some JVM implementation may not even have a garbage collector. e) false Edward [This message has been edited by Edward Man (edited May 21, 2000).]
Edward, You said " b) false. You call System.gc() or Runtime.getRuntime().gc() to "suggest" to the garbage collector to reclaim memory used by unreferenced object. You cannot "run the garbage collector"." but in the Java API it says that Runtime.getRuntime().gc() "Runs the garbage collector. Calling this 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. When control returns from the method call, the Java Virtual Machine has made its best effort to recycle all discarded objects." it runs the garbage collector..doesnt it make letter b true?
The problem is, it's not clear what exactly is meant by "run the garbage collector". It's certainly possible to execute the gc() method, which is part of the overall garbage collection system. However it's not possible to guarantee that any memory will be freed. If gc() exits without really accomplishing anything, did garbage collection "run"? Joy quoted three sentences - note that only the first one actually uses the term "run", and it seems to support Joy's answer. The second sentence tends to support Edward's interpretation, and the third seems to lean again towards Joy - if the JVM "has made its best effort", then it seems to me that garbage collection has "run", even if it failed. But many people seem to think that "run" means "run successfully" in some sense, and I can respect that. This and similarly-worded questions have been much discussed in the past, as you can see using the "search" facility. This particular wording is too ambiguous, IMO, and no definitive answer is really possible. If a question asks if garbage collection can be "invoked" or "called" I'd say yes, definitely. It the question is whether it can be "forced" or "guaranteed" I'd say, certainly not. But for this particular wording, there are valid cases on both sides. We can only hope that Sun doesn't use such poor wording on the actual exam. If they do, drop me a line and I'll drive by their office to throw rocks at their windows in retaliation. Other than that, there doesn't seems to be much else to do about this question - it's not worth worrying about.
I would also cite poor wording in answer e. The way it is written it is surely true. The GC runs in low memory situations. Sometimes it also runs in high memory situations, or middle ones. But there's nothing to say it doesn't run in a low memory situation esp. since it's designed to free up memory. But they probably meant to say only runs in low memory situations, which is false. Eric B.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop