Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak001/weak001.java
64603 views
/*1* Copyright (c) 2004, 2021, 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.OOMStress;65import nsk.share.gc.ThreadedGCTest;66import nsk.share.gc.gp.GarbageProducer;67import nsk.share.gc.gp.GarbageProducerAware;68import nsk.share.gc.gp.GarbageUtils;69import nsk.share.gc.gp.MemoryStrategy;70import nsk.share.gc.gp.MemoryStrategyAware;71import nsk.share.gc.gp.string.InternedStringProducer;72import nsk.share.gc.gp.string.RandomStringProducer;7374public class weak001 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {7576private GarbageProducer garbageProducer;77private MemoryStrategy memoryStrategy;78private InternedStringProducer internedStringProducer = new InternedStringProducer(new RandomStringProducer(10));79// Total number of types to test80final static int TYPES_COUNT = 11;81// Size of array of each tested type. The constant also specifies the82// number of nodes in a NonbranchyTree and size of each node83final static int SIZE = 100;8485protected Runnable createRunnable(int i) {86return new Test();87}8889public void setGarbageProducer(GarbageProducer garbageProducer) {90this.garbageProducer = garbageProducer;91}9293public void setMemoryStrategy(MemoryStrategy memoryStrategy) {94this.memoryStrategy = memoryStrategy;95}9697public static void main(String[] args) {98GC.runTest(new weak001(), args);99}100101// The class implements the logic of the testcase102class Test implements Runnable, OOMStress {103104int iteration;105106public void run() {107// Pre-allocated OOME message to avoid OOME when logging it108String oomMsg = "Ignored OOME in run()";109try {110111log.info("iteration " + iteration);112ReferenceQueue queue = new ReferenceQueue();113WeakReference reference;114int code = iteration % TYPES_COUNT;115String type;116// Define a specific type for each thread to test117switch (code) {118case 0:119reference = new WeakReference(new byte[SIZE], queue);120type = "byte";121break;122case 1:123reference = new WeakReference(new short[SIZE], queue);124type = "short";125break;126case 2:127reference = new WeakReference(new int[SIZE], queue);128type = "int";129break;130case 3:131reference = new WeakReference(new long[SIZE], queue);132type = "long";133break;134case 4:135reference = new WeakReference(new char[SIZE], queue);136type = "char";137break;138case 5:139reference = new WeakReference(new boolean[SIZE], queue);140type = "boolean";141break;142case 6:143reference = new WeakReference(new double[SIZE], queue);144type = "double";145break;146case 7:147reference = new WeakReference(new float[SIZE], queue);148type = "float";149break;150case 8:151reference = new WeakReference(new Object[SIZE], queue);152type = "Object";153break;154case 9:155reference = new WeakReference(internedStringProducer.create(SIZE), queue);156type = "InternedString";157break;158default:159reference = new WeakReference(new NonbranchyTree(SIZE, 0.3f, SIZE),160queue);161type = "NonbranchyTree";162break;163}164int initialFactor = memoryStrategy.equals(MemoryStrategy.HIGH) ? 1 : (memoryStrategy.equals(MemoryStrategy.LOW) ? 10 : 2);165GarbageUtils.eatMemory(getExecutionController(), garbageProducer, initialFactor , 10, 0);166if (!getExecutionController().continueExecution()) {167// we were interrrupted by stresser. just exit...168return;169}170Reference polledReference = null;171try {172polledReference = queue.remove();173} catch (InterruptedException e) {174log.error("Unexpected InterruptedException during queue.remove().");175setFailed(true);176}177// Check the reference and the queue178// The polled reference must be equal to the one enqueued to179// the queue180181if (polledReference != reference) {182log.error("The original reference is not equal to polled reference.");183setFailed(true);184}185186// queue.poll() once again must return null now, since there is187// only one reference in the queue188polledReference = queue.poll();189if (polledReference != null) {190log.error("There are more than one references in the queue.");191setFailed(true);192}193reference.clear();194} catch (OutOfMemoryError e) {195log.info(oomMsg);196}197iteration++;198}199}200}201202203