Question 65: 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 and it will be schedule in due time.. (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. Ans given : b, c and e Question 65: b, c, e. You cannot directly free the memory allocated by an object, though you can set an object reference to null. Also, The garbage collector only runs in low-memory situations, and so does not always reclaim an object�s memory as soon as it becomes a candidate for garbage collection. I agree with C and E but what about B? The exact wording of B seems ambiguos, because you can suggest GC and it may run eventually, or may not at all! The wording "can run" seems to make it false! Am I right?
I would agree that B is false. Unless they consider suggesting the garbage collector run as directly running the garbage collector. Garbage collection may happen in due time, but you can't directly make the vm garbage collect.
hi I feel that B is true because when the garbage collector is run using the command, the system schedules it for garbage collection but the time when it is be executed is not known. So, by the ststement "you can run it whenever u want" the meaning is " run the command to schedule it for gc"... just a thought...
Originally posted by Charles Leung: After careful reading, I think option B is correct. When you called System.gc(), you know it will run in future. Although the exact time is unknown, you may say it is "scheduled".
I disagree. It is not necessarily scheduled, and you are given no guarantees whatsoever that it will actually run before termination of the program. The only garbage collection guarantee the Java language gives you is that the garbage collector will run before an OutOfMemoryError is thrown. Option B, IMHO, is blatantly incorrect. You cannot "directly run the garbage collector whenever you want to". The best you can do is "suggest that the Java virtual machine expend effort toward recycling unused objects". - Peter
The wording of the option B is ambiguous as it attempts to join 2 totally contradictory statements with a conjunction. The first part
You can directly run the garbage collector whenever you want to
attempts to suggest that you can invoke GC any time.. which FALSE. We can only "request" JVM to "run" GC. While the second part
it will be schedule in due time
conveys the right and is TRUE. So, in totality, it seems to me, that the answer is FALSE [ after all TRUE AND FALSE will give FALSE]. We can only hope such ambiguous statements do not appear in the exam
Joined: Mar 02, 2003
Peter & Neeraj, I agree thanks you
Peter den Haan
Joined: Apr 20, 2000
Before deciding whether the second part is true, I wonder what exactly does it mean? Do you have any guarantees that the garbage collector is "scheduled" as such (apart from the concept of general thread scheduling, which is probably not meant here)? What exactly is "due time"? After all, the only real guarantee you have is that the garbage collector will run (not: be scheduled) before an OutOfMemoryError is thrown. Even taken in isolation the second half seems very ambiguous and ill-defined. It can be given a valid meaning, as you do (and there's nothing wrong with that!), but it can easily be interpreted in ways that are not valid at all. At best, we can assign it an UNDEFINED truth value or throw an AmbiguousQuestionException If my experience is anything to go by, the exam is much more carefully worded. - Peter [ April 10, 2003: Message edited by: Peter den Haan ]
Can someone explain what does (c) exactly mean? Thanks.
Basically, that means that the garbage collector will give your object some warning that it is about to be garbage collected. In this case, that warning takes the form of invoking the object's finalize method. That method gives the object a chance to "clean up" anything that it needs to before it is obliterated. I hope that helps, Corey