• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
lucy hu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how C is correct?plz
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO: too much noise for a fuzzy question.
-VG.
 
Bruce Lee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why is "a" incorrect?
 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
the option b is correct .But how come c & e are correct.
please explain
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic