1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @key gc
  28  *
  29  * @summary converted from VM Testbase gc/gctests/FinalizerGC02.
  30  * VM Testbase keywords: [gc]
  31  * VM Testbase readme:
  32  * In this contrived test, the finalizer creates more garbage. As the finalizer
  33  * is invoked prior to garbage collection, this puts more stress on the
  34  * garbage collector.
  35  *
  36  * @library /vmTestbase
  37  *          /test/lib
  38  * @run driver jdk.test.lib.FileInstaller . .
  39  * @run main/othervm gc.gctests.FinalizerGC02.FinalizerGC02
  40  */
  41 
  42 package gc.gctests.FinalizerGC02;
  43 
  44 import nsk.share.TestFailure;
  45 
  46 class node {
  47     byte [] arr;
  48     node next;
  49     node prev;
  50     node(int info){ arr = new byte[100]; }
  51 }
  52 
  53 class CircularLinkedList{
  54    node Root;
  55 
  56    private void addElement(int info) {
  57       node newnode;
  58 
  59       newnode = new node(info);
  60       if (Root == null){
  61         Root = newnode;
  62         Root.next = Root;
  63          Root.prev = Root;
  64       } else {
  65          newnode.next = Root.next;
  66          Root.next.prev = newnode;
  67          Root.next = newnode;
  68          newnode.prev = Root;
  69       }
  70    }
  71 
  72    int elementCount() {
  73       node p;
  74       int count;
  75 
  76        p = Root;
  77        count = 0;
  78 
  79        do {
  80           p = p.prev;
  81           count++;
  82        }while(p != Root);
  83        return count;
  84    }
  85 
  86    public void buildNMegList(int N) {
  87      for (int i = 0 ; i < N*10000 ; i++)
  88         addElement(i);
  89    }
  90 
  91    protected void finalize () {
  92      // generate 1Meg more garbage
  93      FinalizerGC02.listHolder = new CircularLinkedList();
  94      FinalizerGC02.listHolder.buildNMegList(1);
  95    }
  96 }
  97 
  98 
  99 public class FinalizerGC02 {
 100 
 101   static CircularLinkedList listHolder;
 102   static int count;
 103   static int returnCount() { return count; }
 104 
 105   public static void main(String args[]) {
 106     int memory_reserve[] = new int [1000];
 107 
 108     listHolder = new CircularLinkedList();
 109     listHolder.buildNMegList(1);
 110 
 111     try {
 112       while (count < 5) {
 113         listHolder = new CircularLinkedList();
 114         listHolder.buildNMegList(1);
 115         count ++;
 116       }
 117     } catch (OutOfMemoryError e) {
 118        memory_reserve = null;
 119        System.gc();
 120        throw new TestFailure("Test failed at " + count +"th iteration.");
 121     }
 122        System.out.println("Test Passed");
 123     }
 124 
 125     static void doComputation(){
 126       long i = 0;
 127       while ( i < 1000000) {
 128         i ++;
 129       }
 130     }
 131 }