From: lbrtchx on
~
*/javase/6/docs/api/java/lang/management/GarbageCollectorMXBean.html
~
defines .getCollectionCount() and .getCollectionTime() as this interface's methods
~
but what I'd wish some class would have is hooks like:
~
.doRightBeforeCollection(Object ORBC, boolean Do) and .doRightAfterCollection(Object ORAC, boolean Do)
~
Where:
~
1) ORBC and ORAC would be command objects implementing an interface with has as its single method "public void do(){...}"
~
2) in order to protect the GB, ORBC and ORAC could do anything that java offers them, except:
2.1) going native (via JNI or java.lang.RunTIme(<some script>))
2.2) creating objects or calling methods that may interfere with the impending GC work, such as new threads
~
3) the GC -may- decide not to honor "doing", say, ORBC, but if (Do == true), it will at the very least report that it didn't instead of just ignoring the request
~
I think this would be safe for the GC because even if some sloppy coder would not close/ finalize an open input stream, the GC would pick up from there and claim that mess too
~
I think this is great for safe pointing applications that run for a long time such as web servers and it doesn't really mess with the GC (as I see it) with a little help from the all-powerful GC, using -verbose:class with time stamps and using OS scripts, we could sleep a bit better
~
Why hasn't Sun specified such life cycle events in the GC?
~
Thanks
lbrtchx
From: Joshua Cranmer on
On 01/17/2010 08:08 PM, lbrtchx(a)gmail.com wrote:
> but what I'd wish some class would have is hooks like:
> ~
> .doRightBeforeCollection(Object ORBC, boolean Do) and .doRightAfterCollection(Object ORAC, boolean Do)
> ~

How about
<http://java.sun.com/javase/6/docs/api/java/lang/ref/package-summary.html>?
Well, that gets you the "after collection" part (with
PhantomReferences), and I think you can set it up with a WeakReference
to do a "before collection". Granted, it's not *right* before or after,
but there really is no way to do that because of how the GC is threaded.

> I think this would be safe for the GC because even if some sloppy
> coder would not close/ finalize an open input stream, the GC would
> pick up from there and claim that mess too

I believe the input streams override finalize() to call close() anyways.
Well, the File and Socket streams do at least. It seems, on briefly
looking through some major Java classes, that many classes which hold
open native resources override finalize() to close out those resources.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Kevin McMurtrie on
In article <1263776888.904400(a)nntp.aceinnovative.com>,
lbrtchx(a)gmail.com wrote:

> ~
> */javase/6/docs/api/java/lang/management/GarbageCollectorMXBean.html
> ~
> defines .getCollectionCount() and .getCollectionTime() as this interface's
> methods
> ~
> but what I'd wish some class would have is hooks like:
> ~
> .doRightBeforeCollection(Object ORBC, boolean Do) and
> .doRightAfterCollection(Object ORAC, boolean Do)
> ~
> Where:
> ~
> 1) ORBC and ORAC would be command objects implementing an interface with has
> as its single method "public void do(){...}"
> ~
> 2) in order to protect the GB, ORBC and ORAC could do anything that java
> offers them, except:
> 2.1) going native (via JNI or java.lang.RunTIme(<some script>))
> 2.2) creating objects or calling methods that may interfere with the
> impending GC work, such as new threads
> ~
> 3) the GC -may- decide not to honor "doing", say, ORBC, but if (Do == true),
> it will at the very least report that it didn't instead of just ignoring the
> request
> ~
> I think this would be safe for the GC because even if some sloppy coder
> would not close/ finalize an open input stream, the GC would pick up from
> there and claim that mess too
> ~
> I think this is great for safe pointing applications that run for a long
> time such as web servers and it doesn't really mess with the GC (as I see
> it) with a little help from the all-powerful GC, using -verbose:class with
> time stamps and using OS scripts, we could sleep a bit better
> ~
> Why hasn't Sun specified such life cycle events in the GC?
> ~
> Thanks
> lbrtchx

Overriding Object.finalize() will cause the method to be queued for
execution before the object is freed. What ever you do, make sure the
method runs very quickly or there will be a backlog in freeing all
objects overriding finalize().

WeakReference: The referred object turns to null and the WeakReference
is queued after it has been GCed. Subclasses may add data for native
code to perform cleanup.

PhantomReference: The PhantomReference is queued when the referred
object is no longer reachable. Subclasses may add data for native code
to perform cleanup. When the PhantomReference becomes unreachable, the
referred object will be released with it.
--
I won't see Google Groups replies because I must filter them as spam