Calling System.gc( )

Hy,
i have a strong feeling that question regarding garbage collection has one wrong answer.

Here is the question: http://www.irixtech.com/content/exam-2-ques-25
This question was included in Practice Test 2: http://www.irixtech.com/java/scjp/mock-exam/practice-test-2

Following answers were considered to be correct:

  • Calling System.gc( ) will cause eligible objects to be garbage collected.
  • If an object can be accessed from a live thread, then it cannot be garbage collected.

Correct me if I'm wrong, but i think that first of them is not correct as garbage collection can be suggested, but not forced - hence it is not guaranteed that calling System.gc( ) will cause eligible objects to be garbage collected. calling System.gc( ) often causes GC to run.

Tnx in advance,
Ats

Good error you picked up in the mock exam. Per java docs -

"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. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects."

It's now fixed.

Thanks
Ashish Hareet