1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.ListIterator;
/*
* @test
* @requires vm.gc.ConcMarkSweep & !vm.graal.enabled
* @key cte_test
* @bug 4950157
* @summary Stress the behavior of ergonomics when the heap is nearly full and
* stays nearly full.
* @run main/othervm
* -XX:+UseConcMarkSweepGC -XX:-CMSYield -XX:-CMSPrecleanRefLists1
* -XX:CMSInitiatingOccupancyFraction=0 -Xmx80m TestBubbleUpRef 16000 50 10000
*/
/**
* Test program to stress the behavior of ergonomics when the
* heap is nearly full and stays nearly full.
* This is a test to catch references that have been discovered
* during concurrent marking and whose referents have been
* cleared by the mutator.
* Allocate objects with weak references until the heap is full
* Free the objects.
* Do work so that concurrent marking has a chance to work
* Clear the referents out of the weak references
* System.gc() in the hopes that it will acquire the collection
* Free the weak references
* Do it again.
*
* Use the following VM options
* -Xmx80m -XX:-CMSYield [-XX:+UseConcMarkSweepGC] -XX:-CMSPrecleanRefLists1
* -XX:CMSInitiatingOccupancyFraction=0
*
* Use parameter:
* args[0] - array size (16000)
* args[1] - iterations (50)
* args[2] - work (10000)
*/
class MyList extends LinkedList {
int[] a;
MyList(int size) {
a = new int[size];
}
}
class MyRefList extends LinkedList {
WeakReference ref;
MyRefList(Object o, ReferenceQueue rq) {
ref = new WeakReference(o, rq);
}
void clearReferent() {
ref.clear();
}
}
public class TestBubbleUpRef {
MyList list;
MyRefList refList;
ReferenceQueue rq;
int refListLen;
int arraySize;
int iterations;
int workUnits;
TestBubbleUpRef(int as, int cnt, int wk) {
arraySize = as;
iterations = cnt;
workUnits = wk;
list = new MyList(arraySize);
refList = new MyRefList(list, rq);
}
public void fill() {
System.out.println("fill() " + iterations + " times");
int count = 0;
while (true) {
try {
// Allocations
MyList next = new MyList(arraySize);
list.add(next);
MyRefList nextRef = new MyRefList(next, rq);
refList.add(nextRef);
} catch (OutOfMemoryError e) {
// When the heap is full
try {
if (count++ > iterations) {
return;
}
System.out.println("Freeing list");
while (!list.isEmpty()) {
list.removeFirst();
}
System.out.println("Doing work");
int j = 0;
for (int i = 1; i < workUnits; i++) {
j = j + i;
}
System.out.println("Clearing refs");
ListIterator listIt = refList.listIterator();
while (listIt.hasNext()) {
MyRefList next = (MyRefList) listIt.next();
next.clearReferent();
}
System.gc();
System.out.println("Freeing refs");
while (!refList.isEmpty()) {
refList.removeFirst();
}
} catch (OutOfMemoryError e2) {
System.err.println("Out of Memory - 2 ");
continue;
}
} catch (Exception e) {
System.err.println("Unexpected exception: " + e);
return;
}
}
}
/**
* Test entry point.
* args[0] - array size (is the size of the int array in a list item)
* args[1] - iterations (is the number of out-of-memory exceptions before exit)
* args[2] - work (is the work done between allocations)
* @param args
*/
public static void main(String[] args) {
// Get the input parameters.
if (args.length != 3) {
throw new IllegalArgumentException("Wrong number of input argumets");
}
int as = Integer.parseInt(args[0]);
int cnt = Integer.parseInt(args[1]);
int work = Integer.parseInt(args[2]);
System.out.println("<array size> " + as + "\n"
+ "<OOM's> " + cnt + "\n"
+ "<work units> " + work + "\n");
// Initialization
TestBubbleUpRef b = new TestBubbleUpRef(as, cnt, work);
// Run the test
try {
b.fill();
} catch (OutOfMemoryError e) {
b = null; // Free memory before trying to print anything
System.err.println("Out of Memory - exiting ");
} catch (Exception e) {
System.err.println("Exiting ");
}
}
}
|