garbage collector

R

raghu

Why there is no guarentee for System.gc() method will perform garbage
collection for a particular time?
 
Z

zero

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.
 
C

Chris Smith

raghu said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Thomas Hawtin

Chris said:
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
 
C

Chris Smith

Thomas Hawtin said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

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.
 
T

Thomas Hawtin

Chris said:
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
 
J

Jim Sculley

Thomas said:
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.

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/fe81fa982b09ce67
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top