This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
Hi all,

I am confused with the answers given to the two questions, in two different mock exam papers.

The questions are as follows:

I. Which of the following statements are true?
1. The automatic garbage collection of the JVM prevents programs from ever running out of memory
2. A program can suggest that garbage collection be perfomed but not force it.
3. Garbage collection is platform indpendent.
4. An object becomes eligible for garbage collction when all references denoting it are set to null.

Answer to this question are 2, 4.

II. You are concerned that your program may attempt to use more memory than is available . To avoid this situation you want to ensure that the JVM will run its garbage collection just before you start a complex routine. what can you do to be certain that garbage collection will run when you want.

1. You cannot be certain when garbage collection will run
2. use the Runtime.ge() method to force garbage collection.
3. Ensure that all variables you require to be garbage collected are set to null.
4. Use the System.gc() method to force garbage collection.

Answer to this question is 2.

Can any one explain me why only 2nd answer is right in question II.
Thanks in advance.
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Well for the 1st Q .
I think the statement
"An object becomes eligible for garbage collction when all references denoting it are set to null."
is a bit ambiguous.
If u had read k&B then u had gone through the islands of Isolation case
where the references to the objects still exist.
for the 2nd Q, i feel that alternate 1 is correct i.e.
You cannot be certain when garbage collection will run.
As we can only request and can't force Garbage Collection.


Mohit Agarwal.
Would be SCJP.
"The will to win is worthless if u do not have the will to prepare"
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by vishnu priya parimi:

2. A program can suggest that garbage collection be perfomed but not force it.
2. use the Runtime.ge() method to force garbage collection.



Straight from Marcus' tutorial (since the questions came from his mock exam) http://www.jchq.net/tutorial/03_01Tut.htm:

...you can suggest or encourage the JVM to perform garbage collection but you can not force it.

Let me re-state that point, you cannot force garbage collection, just suggest it.
...
You can suggest garbage collection with System.gc(), but this does not guarantee when it will happen

In addition some mock exams talk about calling the garbage collector. System.gc() does indeed call the garbage collection routine, and it gets called immediately and after the call, control is returned to the program. However this does not imply that any objects are guaranteed to be garbage collected.

According to this explanation, the first #2 is true. The second #2 is false. I think everyone would agree that the second #2 is false.

If you look carefully at the answer to #55 (not number 54) on http://www.jchq.net/mockexams/exam1.htm you will see that the correct answer is 1, not 2. Here it is:

Answer 55)
Objective 3.1)
Back to Question 55)

1) You cannot be certain when garbage collection will run

Although there is a Runtime.gc(), this only suggests that the Java Virtual Machine does its garbage collection. You can never be certain when the garbage collector will run. Roberts and Heller is more specific abou this than Boone. This uncertainty can cause consternation for C++ programmers who wish to run finalize methods with the same intent as they use destructor methods.

[ October 04, 2004: Message edited by: Louie van Bommel ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
yes, the correct answer to the 2nd question is 1, nor 2.

Either the author of that mock exam did a poor job editing and proofreading his answers or he didn't understand the garbage collection system.

Problem is that the javadoc for Runtime.gc() is somewhat ambiguous.
If you read JUST the short description in the function list it will indeed hint that it forces the garbage collector to run.
The long description though makes it clear this is not the case and all it does is request garbage collection is run.
And even IF the garbage collector runs that will never ensure enough free memory. It might make things even worse and cause the JVM to crash if there's not enough memory for the garbage collector to run in the first place (though JVMs are supposed to guard against this and attempt to run the garbage collector before memory starvation reaches that point, thus making sure there's always memory to run the garbage collector at least).

Short description:

Runs the garbage collector.


Long description:

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 virtual machine has made its best effort to recycle all discarded objects.


Also, System.gc() doesn't tell AT ALL that the call does not force the garbage collector to run but only requests it:

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


Not the best bit of documentation IMO.


42
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What's more it says this:
When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects


The term "best effort" implies, to me at least, it actually has possibly (or even more likely than not) done some garbage collection at the point of return.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
Thank you all for your reply.

Now i got clarification on System.gc() and Runtime.gc() methods,

But one doubt that is still haunting me is that when the references to objects are set to null, will it become eligible for garbage collection?

-->An object becomes eligible for garbage collection when all references denoting it are set to null.

is it correct in one of the mock exams the first one is correct or

-->Ensure that all variables you require to be garbage collected are set to null.
And in other mock exam this answer is wrong,

Can any one explain me that whether when references to objects are set to null, the object becomes eligible for garbage collection or not.

Thanks in advance.
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi there,



is it correct in one of the mock exams the first one is correct or

-->Ensure that all variables you require to be garbage collected are set to null.



Variables are not garbage collected, only the 'objects' are garbage collected. I think that's the reason the answer in the mock exam seems to be wrong.Coming to the other question, yes when all the references of an object are set to null then that object becomes eligible for garbage collection.

I hope it helps


Impossible is I M Possible
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
As prashant pointed out, the wording seems

Yup, setting ALL (keyword is ALL) references to an object make it eligible.

Two points:
1. If you ensured that ALL reference variables that referenced an object ObjectX were set to null, the object would be eligible for garbage collection.

2. If you set a reference variable x to null, the object it had referenced is not necessarily eligible for garbage collection.

Try this at home [pseudocode alert]

Note here that I set the reference variable x to null, yet the object is not garbage collected.

Setting a reference variable (x) to null doesn't necessarily cause an object (XObject) to be eligible for garbage collection. There could still be other references (variable y) pointing to the object (XObject.

If you were to find all references to the object XObject, and set them ALL to null, the object will be eligible for garbage collection.

And yes, I agree the reason for it not being an acceptable answer is because it says "variables" to be garbage collected.

You do know that Marcus has a much better exam online don't you? Those 3 exams are kinda old and intended for the 1.2 exam. Try username: guest password: password at
http://www.jchq.net/phezam

[ October 05, 2004: Message edited by: Louie van Bommel ]

("variable a" -> "variable x" - Barry)
[ October 06, 2004: Message edited by: Barry Gaunt ]
 
GeeCON Prague 2014
 
subject: Garbage Collection