Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/FinalizerGC02/FinalizerGC02.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/FinalizerGC02.28* VM Testbase keywords: [gc]29* VM Testbase readme:30* In this contrived test, the finalizer creates more garbage. As the finalizer31* is invoked prior to garbage collection, this puts more stress on the32* garbage collector.33*34* @library /vmTestbase35* /test/lib36* @run main/othervm gc.gctests.FinalizerGC02.FinalizerGC0237*/3839package gc.gctests.FinalizerGC02;4041import nsk.share.TestFailure;4243class node {44byte [] arr;45node next;46node prev;47node(int info){ arr = new byte[100]; }48}4950class CircularLinkedList{51node Root;5253private void addElement(int info) {54node newnode;5556newnode = new node(info);57if (Root == null){58Root = newnode;59Root.next = Root;60Root.prev = Root;61} else {62newnode.next = Root.next;63Root.next.prev = newnode;64Root.next = newnode;65newnode.prev = Root;66}67}6869int elementCount() {70node p;71int count;7273p = Root;74count = 0;7576do {77p = p.prev;78count++;79}while(p != Root);80return count;81}8283public void buildNMegList(int N) {84for (int i = 0 ; i < N*10000 ; i++)85addElement(i);86}8788protected void finalize () {89// generate 1Meg more garbage90FinalizerGC02.listHolder = new CircularLinkedList();91FinalizerGC02.listHolder.buildNMegList(1);92}93}949596public class FinalizerGC02 {9798static CircularLinkedList listHolder;99static int count;100static int returnCount() { return count; }101102public static void main(String args[]) {103int memory_reserve[] = new int [1000];104105listHolder = new CircularLinkedList();106listHolder.buildNMegList(1);107108try {109while (count < 5) {110listHolder = new CircularLinkedList();111listHolder.buildNMegList(1);112count ++;113}114} catch (OutOfMemoryError e) {115memory_reserve = null;116System.gc();117throw new TestFailure("Test failed at " + count +"th iteration.");118}119System.out.println("Test Passed");120}121122static void doComputation(){123long i = 0;124while ( i < 1000000) {125i ++;126}127}128}129130131