Rob Clark writes:
> Here's a crazy idea: the whole problem, as I see it, arises because
> the JVM's garbage collector only knows of one resource pool, the
> heap, when in fact there are other resource pools outside the JVM
> that it should also concern itself with. [...]
>
> For example, sun could add to the System class the following method:
>
> public static synchronized void incrementResourceUsage
> (String resourceName, int amount);
>
> [...] If a threshold is exceeded, it causes garbage-collection,
> which will hopefully reduce the amount of the "foobar" resource in
> use.
I think that would work. But I have three comments. (1) Right now,
Java has a way to explictly release external resources. You can
either have a dispose() method in your class and call it at the
appropriate time (the approach Sun took for graphics-related classes),
or you can, as Chris suggests, call the finalizer explicitly at the
appropriate time.
(2) incrementResourceUsage() adds a very specialized feature to all
JVMs, but as you point out, it's difficult to solve all resource
reclamation problems when using a single-purpose tool like "an
unweighted integer sum of usage values exceeds a threshhold."
System.forceGc() would accomplish the same thing much more flexibly:
class ResourceHog {
// ... whatever stuff you would ordinarily put in ResourceHog
protected static int nInstances = 0;
protected static final int nMagicInstanceThreshold = 50;
public ResourceHog ( /* whatever */ ) {
// ... whatever stuff you would ordinarily do
if (++nInstances > nMagicInstanceThreshold) {
// could also be conditional on other things
System.forceGc ();
}
}
public void finalize () { --nInstances; }
}
Or you could write a ResourceManager that would do all sorts of cool
stuff for you.
(3) incrementResourceUsage() would have all the same problems as
System.forceGc(). In other words, it wouldn't work at all for some GC
schemes, including conservative GCs. Conservative GCs never know for
sure that a given reference is live -- they just know when it's dead
(hence "conservative"). So no conservative GC can ever promise to
garbage collect all objects that are at that point garbage. (Not to
mention that "at that point" may be difficult to rigorously define in
a multithreaded system.)
That's why there is no System.forceGc() in Java, why System.gc() is
merely a recommendation, and why incrementResourceUsage() is unlikely
to be implemented.
Best,
daniel dulitz
Valley Technologies, Inc. Peak Performance Real-Time DSP
State College, PA
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]