1 /*
   2  * Copyright (c) 2007, 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  * @test
  26  * @key stress gc
  27  *
  28  * @summary converted from VM Testbase gc/gctests/WeakReference/weak007.
  29  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]
  30  *
  31  * @library /vmTestbase
  32  *          /test/lib
  33  * @run driver jdk.test.lib.FileInstaller . .
  34  * @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.WeakReference.weak007.weak007 -t 1
  35  */
  36 
  37 package gc.gctests.WeakReference.weak007;
  38 
  39 import nsk.share.TestFailure;
  40 import nsk.share.gc.*;
  41 import java.lang.ref.WeakReference;
  42 import java.lang.ref.Reference;
  43 
  44 /**
  45  * Test that GC correctly clears weak references.
  46  *
  47  * This test creates a number of weak references,
  48  * each of which points to the next, then provokes
  49  * GC with Algorithms.eatMemory(). The test succeeds
  50  * if last reference has been cleared and the object
  51  * has been finalized.
  52  */
  53 public class weak007 extends ThreadedGCTest {
  54 
  55     class Worker implements Runnable {
  56 
  57         private int length = 10000;
  58         private int objectSize = 10000;
  59         private Reference[] references;
  60 
  61         private void makeReferences() {
  62             references[length - 1] = null;
  63             FinMemoryObject obj = new FinMemoryObject(objectSize);
  64             references[0] = new WeakReference(obj);
  65             for (int i = 1; i < length; ++i) {
  66                 references[i] = new WeakReference(references[i - 1]);
  67             }
  68             for (int i = 0; i < length - 1; ++i) {
  69                 references[i] = null;
  70             }
  71         }
  72 
  73         public void run() {
  74             makeReferences();
  75             Algorithms.eatMemory(getExecutionController());
  76             if (getExecutionController().continueExecution()) {
  77                 if (references[length - 1].get() != null) {
  78                     throw new TestFailure("Last weak reference has not been cleared");
  79                 }
  80             } else {
  81                 log.info("Completed iterations: " + getExecutionController().getIteration());
  82             }
  83         }
  84 
  85         public Worker() {
  86             log.info("Array size: " + length);
  87             log.info("Object size: " + objectSize);
  88             references = new WeakReference[length];
  89         }
  90     }
  91 
  92     @Override
  93     protected Runnable createRunnable(int i) {
  94         return new Worker();
  95     }
  96 
  97     public static void main(String[] args) {
  98         GC.runTest(new weak007(), args);
  99     }
 100 }