1 /*
   2  * Copyright (c) 2020, Red Hat, Inc. 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 TestWeakReferenceChurn
  27  * @summary Check that -XX:+ShenandoahAggressiveReferenceDiscovery reclaims references
  28  *          even when accessed during marking
  29  * @key gc
  30  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  31  *
  32  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx1g -Xms1g
  33  *      -XX:+UseShenandoahGC -XX:ShenandoahGuaranteedGCInterval=1
  34  *      -XX:+ShenandoahAggressiveReferenceDiscovery
  35  *      TestWeakReferenceChurn
  36  */
  37 
  38 import java.lang.ref.WeakReference;
  39 import java.util.ArrayList;
  40 import java.util.Random;
  41 
  42 public class TestWeakReferenceChurn {
  43 
  44     public static final int NUM_REFS = 1000000;
  45     public static final int NUM_ITERS = 1000;
  46     public static class Payload {
  47         public int foo;
  48         public Payload() {
  49             foo = 0;
  50         }
  51     }
  52 
  53     private static ArrayList<WeakReference<Payload>> refs;
  54     private static Random random;
  55 
  56     public static void main(String[] args) {
  57         refs = new ArrayList<>(NUM_REFS);
  58         random = new Random();
  59         for (int i = 0; i < NUM_REFS; i++) {
  60             refs.add(new WeakReference(new Payload()));
  61         }
  62         int count = 0;
  63         for (int j = 0; j < NUM_ITERS; j++) {
  64             count = 0;
  65             for (int i = 0; i < NUM_REFS; i++) {
  66                 if (accessRef(i)) {
  67                     count++;
  68                 }
  69             }
  70             if (count == 0) {
  71                 break;
  72             }
  73         }
  74         if (count != 0) {
  75             throw new RuntimeException("References not reclaimed");
  76         }
  77     }
  78 
  79     private static boolean accessRef(int idx) {
  80         Payload item = refs.get(idx).get();
  81         if (item != null) {
  82             item.foo = random.nextInt();
  83             return true;
  84         } else {
  85             return false;
  86         }
  87     }
  88 }