• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

GC

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
joy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

joy

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


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.


Joy... As you can see from the quote you found, running gc() only suggests that JVM run garbage collection.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic