1 /* 2 * Copyright (c) 2018, 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 id=default 27 * @summary Test that null references/referents work fine 28 * @requires vm.gc.Shenandoah 29 * 30 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 31 * -XX:+UseShenandoahGC 32 * -XX:+ShenandoahVerify 33 * TestRefprocSanity 34 * 35 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 36 * -XX:+UseShenandoahGC 37 * TestRefprocSanity 38 * 39 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 40 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 41 * TestRefprocSanity 42 */ 43 44 import java.lang.ref.*; 45 46 public class TestRefprocSanity { 47 48 static final long TARGET_MB = Long.getLong("target", 1_000); // 1 Gb allocation 49 static final int WINDOW = 1_000; 50 51 static final Reference<MyObject>[] refs = new Reference[WINDOW]; 52 53 static Object sink; 54 55 public static void main(String[] args) throws Exception { 56 long count = TARGET_MB * 1024 * 1024 / 128; 57 int rIdx = 0; 58 59 ReferenceQueue rq = new ReferenceQueue(); 60 61 for (int c = 0; c < WINDOW; c++) { 62 refs[c] = select(c, new MyObject(c), rq); 63 } 64 65 for (int c = 0; c < count; c++) { 66 verifyRefAt(rIdx); 67 refs[rIdx] = select(c, new MyObject(rIdx), rq); 68 69 rIdx++; 70 if (rIdx >= WINDOW) { 71 rIdx = 0; 72 } 73 74 // Do allocations to force GCs 75 sink = new byte[100]; 76 77 // Drain the refqueue 78 while (rq.poll() != null); 79 } 80 } 81 82 static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) { 83 switch (v % 10) { 84 case 0: return new SoftReference<MyObject>(null); 85 case 1: return new SoftReference<MyObject>(null, rq); 86 case 2: return new SoftReference<MyObject>(ext); 87 case 3: return new SoftReference<MyObject>(ext, rq); 88 case 4: return new WeakReference<MyObject>(null); 89 case 5: return new WeakReference<MyObject>(null, rq); 90 case 6: return new WeakReference<MyObject>(ext); 91 case 7: return new WeakReference<MyObject>(ext, rq); 92 case 8: return new PhantomReference<MyObject>(null, rq); 93 case 9: return new PhantomReference<MyObject>(ext, rq); 94 default: throw new IllegalStateException(); 95 } 96 } 97 98 static void verifyRefAt(int idx) { 99 Reference<MyObject> ref = refs[idx]; 100 MyObject mo = ref.get(); 101 if (mo != null && mo.x != idx) { 102 throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx); 103 } 104 } 105 106 static class MyObject { 107 final int x; 108 109 public MyObject(int x) { 110 this.x = x; 111 } 112 } 113 114 }