File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

lucy hu
Ranch Hand

Joined: Aug 21, 2000
Posts: 60
this one is from BB exam,
Question 65: Which statements about garbage collection are true?
Select all valid answers.
a.You can directly free the memory allocated by an object.
b.You can directly 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.
e.The garbage collector runs in low-memory situations.
The answer is b, c, e.
I'm confused about c, how does the garbage collector inform?
Lucy
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
i think c and e r correct
b is wrong bcos v cant force garbage collection v can only request garbage collection
please coreect me if im wrong
[This message has been edited by Cherry Mathew (edited December 27, 2000).]
Vlad G
Greenhorn

Joined: Dec 28, 2000
Posts: 17
Probably it means that finalize() method will be called on an object before its memory area will be reused. I'd also say b is incorrect...
--Vlad.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Cherry option b is correct. It says nothing about forcing garbage collection but rather it says you can run it directly whenever you want. This is true. You can use System.gc() to run it directly. But you are right, you can't force it. It's pretty much a trick question. You have to be careful with how it's worded.
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
But how C is correct?plz

I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Anjan, before the garabage collector collects an object, it will call the finalize method once for that object. This is how it lets the object know that it is going to collect it. Vlad has already pointed this out though.
[This message has been edited by Sean Casey (edited December 29, 2000).]
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Only c is correct.
b - program does not have any direct control on GB
last - running GB in low memory situation not a must.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Answer b is correct as well.
This is from Khalid and Rasmussen. Section 8.1 p.255
"Java does provide facilities to invoke the garbage collection explicitly. The System.gc() method can be used to force garbage collection, and the System.runFinalization() method van be used to run the finalizers for objects eligible for garbage collection.
Certain aspects regarding garbage collection should be noted:
There is no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. Garbage collection might not even be run if the program execution doestn' warrant it. Thus, any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the operating system or by other means.
There are also no guarantees on the order in which the objects will be garbage collected, or on the order in which their finalizers will be executed. Therefore, the program should not make any decisions based upon these assumptions."

This is why I argue that you can directly run the garbage collector, but it isn't certain if it will actually run or not.
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
That's right.
Let me put it this way:
direct invoking of the garbage collector does not make
"guarantees that the objects no longer in use will be garbage collected ".
But you are right, gb could be explicitely called, and for purpose of this question b is correct.

[This message has been edited by Vladimir Kositsky (edited December 30, 2000).]
Rosemol Thanjappan
Ranch Hand

Joined: Aug 08, 2000
Posts: 40
Hi,

Originally posted by Vladimir Kositsky:
direct invoking of the garbage collector does not make
"guarantees that the objects no longer in use will be garbage collected ".
But you are right, gb could be explicitely called, and for purpose of this question b is correct.
[This message has been edited by Vladimir Kositsky (edited December 30, 2000).]

I also think the Option B is correct.
In the API reference explanation for Class Runtime

_________________________________________________________________

public void 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.
So answer B is True.
Thanking you Rosemol.
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
I don't agree that GC can run at the programmers wish. In option B there is wording "Whenever you want to". System.gc is like starting a car. You can definately start the engine, but have no control on when and how it will start moving, we can't call it's running can we? By calling System.gc, GC gets scheduled as a lowest priority thread, and as for any thread it can't be expected it to execute at programmer's wish.
Thanks
Originally posted by lucy hu:
this one is from BB exam,
Question 65: Which statements about garbage collection are true?
Select all valid answers.
a.You can directly free the memory allocated by an object.
b.You can directly 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.
e.The garbage collector runs in low-memory situations.
The answer is b, c, e.
I'm confused about c, how does the garbage collector inform?
Lucy

Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
I know the last option GC runs in low memory situations is correct but didnt get the implications does it mean that if memory is low runtime wil try to give GC more time to execute
please confirm
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
I'm in total agree with Cherry & Saumil(b is false).Remember that neither GC nor finalization is guaranteed.
With System.gc() you only suggest GC,but not force it to run.
To add, the answer it would have been true if the question was " you can invoke
the g.c. whenever u want"
In MINDQ there is a more clear question(Q37):
a.the garbage collector can be invoked explicitly using a Runtime ogject-True
(Runtime.getRuntime().gc() )
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
Cherry,
Normally GC is run by JVM on it's own, to free up memory. However Programsers have freedom to call System.gc which in turn calls the runtime.getruntime.gc and schedules as a low priroty thread. Since GC is called by VM ist safe to assuem that it will be called to avoid low memory situation.
Thanks
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
b is true. You can directly run the garbage collector whenever you want to.

Here is why I claim so:

Runtime class' contract (in the API doc'n) specifies that gc()'s responsibilty is to run the GC. ("Runs the garbage collector").
It also promises that this is done synchronously ("When control returns from the method call, the Java Virtual Machine has made its best effort to recycle all discarded objects."). This contradicts with what Saumil claims about a low-priority thread doing the GC.

What Runtime's contract does not guarantee is whether the GC when run will perform any actual garbage collection. ("suggests that the Java Virtual Machine expend effort toward recycling unused objects").

Panagiotis.
saumil shukla
Ranch Hand

Joined: Dec 01, 2000
Posts: 47
Hi,
Here is what I found in the API,
gc
public static void gc()
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.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()

See Also:
Runtime.gc()
gc
public void 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.
The name gc stands for "garbage collector". The Java Virtual Machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.
The method System.gc() is hte conventional and convenient means of invoking this method.
Thanks
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
However, e is false (or at least so I believe). Here is the quote that makes me claim so:
"The Java virtual machine specification does not require any particular garbage collection technique. It doesn't even require garbage collection at all.", Bill Venners, "Inside the JVM", Ch. 9.
Of course typical JVM implementation implement GC (for obvious practical reasons), and may also have it run in low-memory situations. e would be true if it were reworded (say) as follows:
e. The garbage collector runs in low-memory situations, in typical/most/existing JVM implementations.
Panagiotis.
Vlad G
Greenhorn

Joined: Dec 28, 2000
Posts: 17
IMHO: too much noise for a fuzzy question.
-VG.
Bruce Lee
Greenhorn

Joined: Nov 20, 2000
Posts: 10

Why is "a" incorrect?
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 141

BEcause we can only free memory through GC and even though we start it.IT is not certain that it will run at the point of time we desired it .
PLease correct me if I am wrong


"Learning is weightless, a treasure you can always carry easily." -Chinese Proverb
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
the option b is correct .But how come c & e are correct.
please explain
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection
 
Similar Threads
GC
Garbage Collection
Booney mock exam q#65
a question about GC
gc