"raghu" <raghujindia@gmail.com> wrote in news:1134927095.209719.312850
@g44g2000cwa.googlegroups.com:
> Why there is no guarentee for System.gc() method will perform garbage
> collection for a particular time?
So your teacher gave you a list of questions and expects you to think about
them and come up with a good answer, correct? May I suggest you actually
do think about them, instead of asking us. Perhaps if you at least show
that you thought about it by posting your thoughts, the group would be more
likely to point you in the right direction.

Signature
Beware the False Authority Syndrome
> Why there is no guarentee for System.gc() method will perform garbage
> collection for a particular time?
Because it may not be possible for garbage collection work to be done
right now. The virtual machine, languagem, and API specifications leave
open the possibility for a wide variety of garbage collection
implementations, and so no assumption is made that garbage collection is
possible at arbitrary times.
The contract for System.gc() makes two discrete guarantees:
1. "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."
2. "When control returns from the method call, the Java Virtual Machine
has made a best effort to reclaim space from all discarded objects."
It's distressingly common to see people quote #1 rather ferociously, and
then ignore #2. So to reiterate, you are GUARANTEED that when the
system returns from System.gc(), a best effort has been made to perform
any garbage collection work that can be done. The two statements --
"suggests" and "best effort" -- are reconciled in that it's possible
that no garbage collection effort is possible at all, in which case a
best effort is equivalent to no effort at all.
How is that possible? Two ways, that I can think of:
1. Perhaps there is never any deferred garbage collection work in this
implementation of the JVM. For example, perhaps it's a variant on
reference counting with magic to collect cycles deterministically.
2. Perhaps situations aren't right... for example, maybe it's a
distributed JVM, and there's a network link down that prevents it from
scanning the stacks of threads that are running on a different host.
The definition of "best effort" is left rather vague, as well. Since
software is written in advance, you could argue (circularly, of course,
and rather unconvincingly) that a software's best effort always consists
of doing whatever its code tells it to do... and thus any JVM conforms
to that requirement by definition. However, most people would recognize
that as word games. In the end, it comes down to the intuition of the
virtual machine implementor what corresponds to a "best effort" of the
existing software, versus changing the software itself. I'd say that if
a garbage collection can be performed using the exposed API of the
garbage collection module (however that's defined), then the VM is
required to do it by the API specification.
In practice, all common used garbage collectors do run garbage
collection in response to a call to System.gc(). Barring unusual
circumstances, it would almost surely be a violation of the API contract
not to do so.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Thomas Hawtin - 18 Dec 2005 18:42 GMT
> It's distressingly common to see people quote #1 rather ferociously, and
> then ignore #2. So to reiterate, you are GUARANTEED that when the
> system returns from System.gc(), a best effort has been made to perform
> any garbage collection work that can be done. The two statements --
Perhaps it's just people with long memories of when System.gc was fast.
Then one version, programs that call it lots slowed to a crawl.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Chris Smith - 18 Dec 2005 19:06 GMT
> Perhaps it's just people with long memories of when System.gc was fast.
> Then one version, programs that call it lots slowed to a crawl.
I've been working in Java since the summer of 1997, and I don't recall
its ever being particularly fast. Not that I write much code that calls
it.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Thomas Hawtin - 19 Dec 2005 10:13 GMT
> I've been working in Java since the summer of 1997, and I don't recall
> its ever being particularly fast. Not that I write much code that calls
> it.
I have a memory of a thread posted hereabouts around the 1.2->1.3 era
(possibly) revolving around, IIRC, an image process library. The code
slowed down significantly when upgrading. IIRC, it turned out that
System.gc used to pretty much be ignored if there had just been a
significant collection. The code called System.gc often, so with the old
version it ignored most of them. However, I can't find anything relevant
on groups.google.com.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Jim Sculley - 19 Dec 2005 13:42 GMT
>> I've been working in Java since the summer of 1997, and I don't recall
>> its ever being particularly fast. Not that I write much code that
[quoted text clipped - 7 lines]
> version it ignored most of them. However, I can't find anything relevant
> on groups.google.com.
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/fe
81fa982b09ce67

Signature
Remove my extraneous mandibular appendages to reply via email.
>Why there is no guarentee for System.gc() method will perform garbage
>collection for a particular time?
To protect you from foolishness. There is no point in doing GC unless
you would actually get substantial RAM back.. You as programmer
can't know that.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.