diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/finalize/CrashedFinalizable.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/finalize/CrashedFinalizable.java new file mode 100644 index 0000000000..d5ee27591b --- /dev/null +++ b/core-java-modules/core-java-lang/src/main/java/com/baeldung/finalize/CrashedFinalizable.java @@ -0,0 +1,28 @@ +package com.baeldung.finalize; + +import java.lang.ref.ReferenceQueue; +import java.lang.reflect.Field; + +public class CrashedFinalizable { + public static void main(String[] args) throws ReflectiveOperationException { + for (int i = 0; ; i++) { + new CrashedFinalizable(); + if ((i % 1_000_000) == 0) { + Class finalizerClass = Class.forName("java.lang.ref.Finalizer"); + Field queueStaticField = finalizerClass.getDeclaredField("queue"); + queueStaticField.setAccessible(true); + ReferenceQueue referenceQueue = (ReferenceQueue) queueStaticField.get(null); + + Field queueLengthField = ReferenceQueue.class.getDeclaredField("queueLength"); + queueLengthField.setAccessible(true); + long queueLength = (long) queueLengthField.get(referenceQueue); + System.out.format("There are %d references in the queue%n", queueLength); + } + } + } + + @Override + protected void finalize() { + System.out.print(""); + } +}