8276422: Add command-line option to disable finalization

Co-authored-by: David Holmes <dholmes@openjdk.org>
Co-authored-by: Brent Christian <bchristi@openjdk.org>
Reviewed-by: dholmes, kbarrett, bchristi
This commit is contained in:
Stuart Marks 2021-12-08 00:27:53 +00:00
parent ec7cb6d5d3
commit d7ad546758
13 changed files with 265 additions and 17 deletions

View file

@ -61,9 +61,17 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
return queue;
}
static final boolean ENABLED = isFinalizationEnabled();
private static native boolean isFinalizationEnabled();
/* Invoked by VM */
static void register(Object finalizee) {
new Finalizer(finalizee);
if (ENABLED) {
new Finalizer(finalizee);
} else {
throw new InternalError("unexpected call to Finalizer::register when finalization is disabled");
}
}
private void runFinalizer(JavaLangAccess jla) {
@ -130,7 +138,7 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
/* Called by Runtime.runFinalization() */
static void runFinalization() {
if (VM.initLevel() == 0) {
if (VM.initLevel() == 0 || ! ENABLED) {
return;
}
@ -182,14 +190,16 @@ final class Finalizer extends FinalReference<Object> { /* Package-private; must
}
static {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
Thread finalizer = new FinalizerThread(tg);
finalizer.setPriority(Thread.MAX_PRIORITY - 2);
finalizer.setDaemon(true);
finalizer.start();
if (ENABLED) {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
Thread finalizer = new FinalizerThread(tg);
finalizer.setPriority(Thread.MAX_PRIORITY - 2);
finalizer.setDaemon(true);
finalizer.start();
}
}
}

View file

@ -194,7 +194,10 @@ java.launcher.X.usage=\n\
\ override or augment a module with classes and resources\n\
\ in JAR files or directories.\n\
\ --source <version>\n\
\ set the version of the source in source-file mode.\n\n\
\ set the version of the source in source-file mode.\n\
\ --finalization=<value>\n\
\ controls finalization\n\
\ <value> is one of "enabled" or "disabled"\n\n\
These extra options are subject to change without notice.\n
# Translators please note do not translate the options themselves