This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes from Marcus Green mock 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 "from Marcus Green mock" Watch "from Marcus Green mock" New topic

from Marcus Green mock

Vijay pillai
Ranch Hand

Joined: Jan 10, 2001
Posts: 45

hello friends,
I found this Q in Marcus Green(exam 1)
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 Java Virtual Machine 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.gc() method to force garbage collection
3) Ensure that all the variables you require to be garbage collected are set to null
4) Use the System.gc() method to force garbage collection
I am slightly confused about the answer,the ans is 1.
What I feel is that a call to System.gc() or Runtime.gc(),will definetly run the Garbage collector ,But we cannot be sure whether the Object will actully be Garbage collected.
but the point is we can run gc when we opt to.
In that case,ans one cannot be correct all we can do in such a scenario is remove all the references to the Object and ensure that that particular Object is no longer referenced,and call either of the two methods and pray.
but how can ans one be taken as a right one,pls help me.
Or am I not getting it right
Vijay Pillai
mahesh deshpande

Joined: Oct 26, 2000
Posts: 19
Dear Vijay,
As per the question the ans. 1 is correct. It's asked, what can you do to be certain that garbage collection will run when you want.So the answer should be, we can't do anything. It's a low priority thread when it will be executed is not certain.
But I am bit confused about first part of your question i.e. you want to clear meomery before start of program ?. As per my understanding GC and release of meomery is during the execution of the program. Am I right or missing something.
Vijay pillai
Ranch Hand

Joined: Jan 10, 2001
Posts: 45
hi again,
can someone expain this.
we can make the Garbage collector run with a call to System.gc,Irrespective to whether the Object will actually be Garbage collected or not.
true or false.
if true then how can the answer to the previous Q be 1.
and if false,I need some help in this topic.
May be someone with a better understanding of GC could explain.
Vijay Pillai
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vijay,
You are wrong! The call to System.gc() is only a HINT to the JVM that we would like it to run garbage collection. The JVM is free to totally ignore the call and from experience that is mostly the case (unless we have a low memory situation). Therefore, no programmer can be CERTAIN when garbage collection will be run. The JVM specification leaves it up to the JVM developers so even they are not CERTAIN!
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
I'm just curious, in previous versions of JAVA could you ever depend on System.gc() to work? There are a few gc related exercises in the Bruce Eckel book (Thinking in Java, chapter 4) that drove me crazy when they wouldn't function consistantly. I was thinking that my code was bad until I considered that maybe the System.gc() wasn't working when I wanted it to. Then again, maybe that was the point of the exercise!
Pat B.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: from Marcus Green mock
It's not a secret anymore!