Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak001/weak001.java
41161 views
/*1* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/WeakReference/weak001.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29* VM Testbase readme:30* DESCRIPTION31* The test checks that Garbage Collector correctly works with32* WeakReferences. It also checks that no unexpected exceptions and errors33* are thrown or the JVM is not crashed.34* The test starts a number of threads. Each thread run tests for some time35* or serveral iterations. See javadoc StressOptions for configuration.36* First of all each test defines what type to check (there are 10 types37* totally). As soon as the type is defined, a WeakReference is created that38* refers to an array of tested type and is registered with in a queue. A39* WeakReference for NonbranchyTree class does not refer to an array, but to40* instances of the class.41* After that a thread performs next checks for the reference:42* 1. The reference is in queue after GC is provoked with43* Algorithms.eatMemory() method (a single thread eats the memory).44* 2. queue.remove() returns reference from the queue.45* 3. queue.poll() returns null.46* 4. reference.clear() does not throw any exception.47* The test extends ThreadedGCTest and implements GarbageProducerAware and48* MemoryStrategyAware interfaces. The corresponding javadoc documentation49* for additional test configuration.50*51* @library /vmTestbase52* /test/lib53* @run main/othervm gc.gctests.WeakReference.weak001.weak001 -ms low54*/5556package gc.gctests.WeakReference.weak001;5758import java.lang.ref.Reference;59import java.lang.ref.ReferenceQueue;60import java.lang.ref.WeakReference;6162import nsk.share.gc.GC;63import nsk.share.gc.NonbranchyTree;64import nsk.share.gc.ThreadedGCTest;65import nsk.share.gc.gp.GarbageProducer;66import nsk.share.gc.gp.GarbageProducerAware;67import nsk.share.gc.gp.GarbageUtils;68import nsk.share.gc.gp.MemoryStrategy;69import nsk.share.gc.gp.MemoryStrategyAware;70import nsk.share.gc.gp.string.InternedStringProducer;71import nsk.share.gc.gp.string.RandomStringProducer;7273public class weak001 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {7475private GarbageProducer garbageProducer;76private MemoryStrategy memoryStrategy;77private InternedStringProducer internedStringProducer = new InternedStringProducer(new RandomStringProducer(10));78// Total number of types to test79final static int TYPES_COUNT = 11;80// Size of array of each tested type. The constant also specifies the81// number of nodes in a NonbranchyTree and size of each node82final static int SIZE = 100;8384protected Runnable createRunnable(int i) {85return new Test();86}8788public void setGarbageProducer(GarbageProducer garbageProducer) {89this.garbageProducer = garbageProducer;90}9192public void setMemoryStrategy(MemoryStrategy memoryStrategy) {93this.memoryStrategy = memoryStrategy;94}9596public static void main(String[] args) {97GC.runTest(new weak001(), args);98}99100// The class implements the logic of the testcase101class Test implements Runnable {102103int iteration;104105public void run() {106// Pre-allocated OOME message to avoid OOME when logging it107String oomMsg = "Ignored OOME in run()";108try {109110log.info("iteration " + iteration);111ReferenceQueue queue = new ReferenceQueue();112WeakReference reference;113int code = iteration % TYPES_COUNT;114String type;115// Define a specific type for each thread to test116switch (code) {117case 0:118reference = new WeakReference(new byte[SIZE], queue);119type = "byte";120break;121case 1:122reference = new WeakReference(new short[SIZE], queue);123type = "short";124break;125case 2:126reference = new WeakReference(new int[SIZE], queue);127type = "int";128break;129case 3:130reference = new WeakReference(new long[SIZE], queue);131type = "long";132break;133case 4:134reference = new WeakReference(new char[SIZE], queue);135type = "char";136break;137case 5:138reference = new WeakReference(new boolean[SIZE], queue);139type = "boolean";140break;141case 6:142reference = new WeakReference(new double[SIZE], queue);143type = "double";144break;145case 7:146reference = new WeakReference(new float[SIZE], queue);147type = "float";148break;149case 8:150reference = new WeakReference(new Object[SIZE], queue);151type = "Object";152break;153case 9:154reference = new WeakReference(internedStringProducer.create(SIZE), queue);155type = "InternedString";156break;157default:158reference = new WeakReference(new NonbranchyTree(SIZE, 0.3f, SIZE),159queue);160type = "NonbranchyTree";161break;162}163int initialFactor = memoryStrategy.equals(MemoryStrategy.HIGH) ? 1 : (memoryStrategy.equals(MemoryStrategy.LOW) ? 10 : 2);164GarbageUtils.eatMemory(getExecutionController(), garbageProducer, initialFactor , 10, 0);165if (!getExecutionController().continueExecution()) {166// we were interrrupted by stresser. just exit...167return;168}169Reference polledReference = null;170try {171polledReference = queue.remove();172} catch (InterruptedException e) {173log.error("Unexpected InterruptedException during queue.remove().");174setFailed(true);175}176// Check the reference and the queue177// The polled reference must be equal to the one enqueued to178// the queue179180if (polledReference != reference) {181log.error("The original reference is not equal to polled reference.");182setFailed(true);183}184185// queue.poll() once again must return null now, since there is186// only one reference in the queue187polledReference = queue.poll();188if (polledReference != null) {189log.error("There are more than one references in the queue.");190setFailed(true);191}192reference.clear();193} catch (OutOfMemoryError e) {194log.info(oomMsg);195}196iteration++;197}198}199}200201202