Lightweight finalization for the JDK without any drawbacks was: Why is finalize wrong?
Jaroslav Tulach
jaroslav.tulach at oracle.com
Wed Aug 13 11:12:43 UTC 2014
Dne So 9. srpna 2014 07:18:54, Doug Lea napsal(a):
> > Dne Pá 8. srpna 2014 15:02:41, Stanimir Simeonoff napsal(a):
> >
> >
> > According to articles provided by Andrew[1], toString() can be on stack,
> > even if obj
> > (e.g. this for toString()) is garbage collected. This is a bit surprising
> > (an OOP fan
> > would expect that this is GC root while its instance method is running),
> > but such
> > behavior is said to be in accordance to the 1.8 spec.
> >
> > Question for Doug (as a member of jmm-dev list) then is: would not it be
> > wise to
> > change the spec to require this to be GC root while its instance method is
> > being
> > executed?
>
> Possible changes are being discussed. It is not clear yet
> exactly what rules would better match people's expectations
> while remaining implementable by JVMs, and/or whether they
> would force performance degradations even
> for the vast majority of code not using finalizers. It is
> very possible the end result will just be to clarify rules and
> provide mechanisms allowing better programmer control.
>
> Any changes would go into effect for JDK9 at the earliest,
> so if you can find a way to avoid use of finalizers
> here, it would be a better option.
I'll do my best to come up with a lightweight alternative to finalization.
However:
As far as I understand Andrew's inquiry, the problem is not (that much)
related to finalizers, rather to question whether following method can ever
finish or not:
private void gcThis() {
Reference<?> ref = new WeakReference<>(this);
while (ref.get() != null) {
System.gc();
}
System.err.println("ref is gone: " + ref);
}
On Oracle's JDK8 the method has never finished for me. However when I tried
IBM's JDK (identification can be found below) the method finished every time! A
bit of consistency would be beneficial in this area, imho.
-jt
PS: While I understand why the two JDKs behave the way they behave, I don't
think this is explainable to regular Java developers. But maybe they don't
care that much and one day it will be easy to google out solution on
stackoverflow to prevent "this" being garbage collected by changing the last
method line to
System.err.println("ref is gone: " + ref + " for " + this);
The version of IBM's Java was:
~/bin/ibmjava7$ ./bin/java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build pxa6470_27sr1fp1-20140708_01(SR1 FP1))
IBM J9 VM (build 2.7, JRE 1.7.0 Linux amd64-64 Compressed References
20140707_205525 (JIT enabled, AOT enabled)
J9VM - R27_Java727_SR1_20140707_1408_B205525
JIT - tr.r13.java_20140410_61421.07
GC - R27_Java727_SR1_20140707_1408_B205525_CMPRSS
J9CL - 20140707_205525)
JCL - 20140707_01 based on Oracle 7u65-b16
More information about the core-libs-dev
mailing list