Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/FinalizerGC01/FinalizerGC01.java
41159 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26*27* @summary converted from VM Testbase gc/gctests/FinalizerGC01.28* VM Testbase keywords: [gc]29* VM Testbase readme:30* This rather contrived test checks the interplay of garbage collection31* and finalizers. Just before a 0.5 Meg linked list is garbage collected,32* the finalizer generates more garbage . The test fails if an OutOfMemoryError33* is thrown.34*35* @library /vmTestbase36* /test/lib37* @run main/othervm gc.gctests.FinalizerGC01.FinalizerGC0138*/3940package gc.gctests.FinalizerGC01;4142import nsk.share.TestFailure;4344class node {45byte [] arr;46node next;47node prev;48node(int info){ arr = new byte[128]; }49}5051class CircularLinkedList{52private void addElement(int info) {53node newnode;54newnode = new node(info);55if (Root == null){56Root = newnode;57Root.next = Root;58Root.prev = Root;59} else{60newnode.next = Root.next;61Root.next.prev = newnode;62Root.next = newnode;63newnode.prev = Root;64}65}66int elementCount() {67node p;68int count;69p = Root;70count = 0;71do {72p = p.prev;73count++;74}while(p != Root);75return count;76}77public void build1MegList() {78for (int i = 0 ; i < NELEMENTS ; i++)79addElement(i);80}81node Root=null;82static final int NELEMENTS=4096;83}84class CircularLinkedListFinal extends CircularLinkedList {85protected void finalize () {86CircularLinkedList cl = null;87// generate 1.5Meg more garbage88int i = 0;89int gcFails=0;90while (i < 3){91Root = null;92gcFails=0;93while (gcFails < NGCFAILS) {94try {95cl = new CircularLinkedList();96cl.build1MegList();97cl = null;98break;99}100catch (OutOfMemoryError e) {101System.gc();102gcFails++;103}104}105if (gcFails >= NGCFAILS) break;106i++;107}108if (i < 3) throw new OutOfMemoryError();109}110private static final int NGCFAILS=10;111}112113public class FinalizerGC01 {114public static void main(String args[]) {115int count = 0;116int gcFails = 0;117CircularLinkedListFinal cl = null;118while (count < 64) {119gcFails = 0;120while (gcFails<NGCFAILS) {121try {122cl = new CircularLinkedListFinal();123cl.build1MegList();124doComputation();125cl = null;126break;127} catch (OutOfMemoryError e) {128gcFails++;129System.gc();130}131}132if (gcFails >= NGCFAILS) break;133count++;134}135if (count < 64)136throw new TestFailure("Test failed on " + count + " iteration");137else138System.out.println("Test Passed");139}140static void doComputation(){141long i = 0;142while ( i < 1000000L) { i++; }143}144private static final int NGCFAILS=10;145}146147148