There is no more references to the object.From this, it can be assumed that the “stuck” finalize () will hang up the garbage collector stream, and the build will stop. In fact (at least in HotSpot 1.6), the garbage collector does not call finalize () methods directly, but only adds the corresponding objects to a special list by calling the static java.lang.ref.Finalizer.register (Object) method. The object of the Finalizer class is a reference to the object for which you need to call finalize (), and stores references to the next and previous Finalizer, creating a doubly linked list.
static class BigObject { char[] tmp = new char[10000]; }
public static void main(String... args) { int i=0; while(true) { new BigObject(); try { Thread.sleep(10); } catch( InterruptedException e ) {} if(i++%100==0) System.out.println("Total: "+Runtime.getRuntime().totalMemory()+ "; free: "+Runtime.getRuntime().freeMemory()); } }
Create an object at each iteration, and once every hundred iterations we display information about the remaining memory. Let's limit the memory of the Java-machine so as not to delay the tests, and see the result:$ java -Xms16m -Xmx16m Test
Total: 16252928; free: 15965064
Total: 16252928; free: 14013136
Total: 16252928; free: 12011536
Total: 16252928; free: 14309664
Total: 16252928; free: 12308064
Total: 16252928; free: 14797440
Total: 16252928; free: 12795840
Total: 16252928; free: 15307784
...
static class LongFinalize { protected void finalize() throws Throwable { System.out.println("LongFinalize finalizer"); Thread.sleep(10000000); } }
Add new LongFinalize () to main () before the loop. The result will be:$ java -Xms16m -Xmx16m Test
Total: 16252928; free: 15965064
Total: 16252928; free: 14003496
Total: 16252928; free: 12001896
LongFinalize finalizer
Total: 16252928; free: 14290408
Total: 16252928; free: 12288808
Total: 16252928; free: 14777432
Total: 16252928; free: 12775832
Total: 16252928; free: 15286960
Total: 16252928; free: 13280880
static class BigObject { char[] tmp = new char[10000]; protected void finalize() throws Throwable { tmp[0] = 1; } }
This time the picture is different:$ java -Xms16m -Xmx16m Test
Total: 16252928; free: 15965064
Total: 16252928; free: 14003496
Total: 16252928; free: 12001896
LongFinalize finalizer
Total: 16252928; free: 9996648
Total: 16252928; free: 7987144
Total: 16252928; free: 6459728
Total: 16252928; free: 4458128
Total: 16252928; free: 6357016
Total: 16252928; free: 4347352
Total: 16252928; free: 2331112
Total: 16252928; free: 329512
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Test$BigObject.<init>(Test.java:12)
at Test.main(Test.java:31)
static class SubBigObject extends BigObject { protected void finalize() throws Throwable { } }
And create child class objects in main () (replace new BigObject () with new SubBigObject ()). We will see that garbage collection is successful again. public final class Test { static class LongFinalize { protected void finalize() throws Throwable { System.out.println("LongFinalize finalizer"); Thread.sleep(10000000); } } static class BigObject { char[] tmp = new char[10000]; protected void finalize() throws Throwable { tmp[0] = 1; } } public static void main(String... args) { int i=0; new LongFinalize(); while(true) { new BigObject(); try { Thread.sleep(10); } catch( InterruptedException e ) {} if(i++%100==0) System.out.println("Total: "+Runtime.getRuntime().totalMemory()+ "; free: "+Runtime.getRuntime().freeMemory()); if(Runtime.getRuntime().freeMemory()<1e6) System.runFinalization(); } } }
$ java -Xms16m -Xmx16m Test
Total: 16252928; free: 15965064
Total: 16252928; free: 14003496
Total: 16252928; free: 12001896
LongFinalize finalizer
Total: 16252928; free: 9996648
Total: 16252928; free: 7987144
Total: 16252928; free: 6459832
Total: 16252928; free: 4458232
Total: 16252928; free: 6357120
Total: 16252928; free: 4347456
Total: 16252928; free: 2331216
Total: 16252928; free: 239072
Total: 16252928; free: 11729800
Total: 16252928; free: 9717584
Total: 16252928; free: 7719416
Total: 16252928; free: 5710768
Total: 16252928; free: 3721880
Total: 16252928; free: 1710824
Total: 16252928; free: 11261488
Source: https://habr.com/ru/post/144544/
All Articles