aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes One Question about GC 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 "One Question about GC" Watch "One Question about GC" New topic
Author

One Question about GC

katikareddy
Greenhorn

Joined: Jun 12, 2000
Posts: 5
I have one doubt about this question.
Which statements about gc are true?
a)You can directly free the memory allocated by an object.
b)You can run the garbage collector whenever you want.
c)The garbage collector informs your object when it is about to be garbage collected.
d)The garbage collector runs in low-memory situations.
Answer given is B and C.
But my answer is C only.
Can any one clear this question pls.
Regards,
Vijay
Yuhri Hirata
Greenhorn

Joined: Jun 13, 2000
Posts: 10
Check your APIs:
java.lang.System
static void gc()
Suggests to the JVM that it run the garbage collector. Note that it only 'suggests'! ...but in theory, it does give you a measure of control over its running.
Ram Nath
Greenhorn

Joined: Jun 04, 2000
Posts: 29
only c is true. b is not true. GC can be invoked but not run.
You can call System.gc() or Runtime.getRuntime.gc() to invoke gc but no guarntee that it will run.
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
where is that question from what test and question number
and amybe it can be added to the mock exam errarta
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
If I had to answer this question, I would pick c and d as the correct answers.
a)You can directly free the memory allocated by an object.
This is obciously false!!!
b)You can run the garbage collector whenever you want.
You can only suggest that the garbage collector be run, but you can never run it at your will.
c)The garbage collector informs your object when it is about to be garbage collected.
This is VERY TRUE.
d)The garbage collector runs in low-memory situations.
IMHO, this is true also because in fact the garbage collector never runs until the runtime establishes that your program has exhausted the memory allocated to it on the heap and then requests the GC to free some resources if possible.
Herbert
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Just wanted to add myself to the confusion ....
I would go with 'c' for sure. Option 'b' is debatable.
'a' and 'd' are positively not correct. Nothing abt GC
is guarenteed, so I would not choose 'd'.
I would not choose 'b' as the answer. As mentioned above,
you can only suggest GC to be 'run' by invoking it.
However, you cannot run the GC.

Regds.
- satya
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Satya....everyone,
I think that GC will definitely run in low memory situations.We cannot guarantee when GC will be run because we can never tell when our program will run out of memory.But when it does run out of memory, the runtime will definitely invoke the garbage collector, which will eventually be given a chance by the thread scheduler to run. The whole point of automatic garbage collection is to relieve the programmer of the burden to manually keep track of memory usage on the heap.And the way that the runtime decides to invoke the GC is to check the available memory for your program on the heap.When it establishes that your program has no more memory, it has to invoke GC in any case, otherwise it would be unfair on us who would otherwise do the weird C++ malloc and dealloc to to ensure that we are not running out of memory. I insist that GC will definately run in low memory situations(Not system memory, but the memory allocated to your program on the heap).
Herbert
katikareddy
Greenhorn

Joined: Jun 12, 2000
Posts: 5
Hi Herbert,
GC will not run in low memory situations but it will run as Low-Priority thread.I think you got confused with it.
Just check it.
This question is from a sample test.
Regards,
Vijay
rexian
Greenhorn

Joined: May 02, 2000
Posts: 25
garbage collector is invoked if your program runs out of gas, i mean, memory....that's what Sun's brochure says! I will go with C & D.
So far as low priority thread is concerned...yes, gc runs on low priority thread


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR><B>Successful people in this world are those who get up and look for the circumstances they want. If they can't find them, they make them.</B><HR></BLOCKQUOTE>
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Katikaredy,
I know that GC runs as a low priority thread, but that has nothing to do with the memory issue I am trying to discuss here.Really I am not confused with the two.
Herbert.
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Katikareddy,
Here is what the Sun tutorial says about GC and low memory situations.
----------------------------------------------------------------
The garbage collector runs in a low-priority thread and runs either synchronously or asynchronously depending on the situation and the system on which Java is running. It runs synchronously WHEN THE SYSTEM RUNS OUT OF MEMORY or in response to a request from a Java program.
The Java garbage collector runs asynchronously when the system is idle, but it does so only on systems, such as Windows 95/NT, that allow the Java runtime environment to note when a thread has begun and to interrupt another thread. As soon as another thread becomes active, the garbage collector is asked to get to a consistent state and terminate.
-----------------------------------------------------------------
You can get the complete tutorial HERE
Regards,
Herbert
Ihor Strutynskyj
Greenhorn

Joined: Mar 28, 2000
Posts: 22
I have a few comments about questions b) and d). b) is worded a bit confusing. I think it should say: "You can directly invoke the garbage collector whenever you want." We all know that invoking means "suggest" and that DOS NOT guarantee the GC will RUN immediately (or at all) as a result of this call.
For question d): Does it mean ALWAYS or SOMETIMES? If it means
garbage collector SOMETIMES runs in low-memory situations - then d) is true. If it means ALWAYS - then it's false. You can easily write a program that will consume all available memory resources without even giving a chance for GC to start. In this case your application will halt.
--Ihor
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
If you are talking about a program that does not drop any references for its entire lifetime, then yes, it is possible to have such a program exhaust all its memory without GC taking action,since GC does not collect resources for objects that are in use.Such a program would generally have no remedy, even in C or C++ where memory mangement is manual since you dont have anything to free anyway.But if you are talking about a normal program, that drops references,then it does not make sense that such a program can exhaust all its memory without GC doing anything about it, when there are so many objects consuming resources they dont need.If this were possible, then the whole purpose of automatic GC is defeated and it would be better to let the programmer manage memory manually as in C or C++.
Herbert
Rose
Greenhorn

Joined: Sep 08, 2002
Posts: 6
Hello Herbert ,

As read all discusion I found that all are sure answer c is correct.
Where did you got this information?
I wish to know a little bit more about this .

Thanking u

Rose
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie Rose,
Every good Java Certification book will have a section on Garbage Collection because it is among the objectives upon which the certification exam is based.If you are using a general java book, you need to get one that is certification oriented.The link I posted above also has a tutorial on Garbage Collection.

Regards,
Herbert
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Rose- the way GC "informs your object when it is about to be garbage collected" is that it calls the finalize() method. It is guaranteed that this will happen before garbage collection (though not necessarily immediately before). It's also possible that an object is never collected, but is nonetheless finalized. But it it never possible for an object to be collected without first being finalized.
For the record, my answers are B, C, and D - but I acknowledge that B depends on your definition of "run" (I say GC can "run" without getting any results), and D depends not only on that definition but also more importantly on whether you think they mean "always" or "often" (or "sometimes"). It's a poorly written question which can never be answered unambiguously, IMO.


"I'm not back." - Bill Harding, Twister
rexian
Greenhorn

Joined: May 02, 2000
Posts: 25
Agree to Jim on C & D only. For B, "You can suggest JVM to collect an object to the garbage by dropping reference to it,ie, by assigning null value."
Its truly a vaguely asked question. If I sit for Sun's exam, my answer is C & D. For any other C. D is one of the main selling points of Sun's Java :-).
- Rexian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One Question about GC