Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/ThreadGC/ThreadGC.java
41155 views
/*1* Copyright (c) 2002, 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*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM Testbase gc/gctests/ThreadGC.29* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]30* VM Testbase readme:31* This tests attempts to stress the garbage collector my making32* synchronous calls to the garbage collector after producing garbage.33* The garbage collector is invoked in a separate thread.34* The test runs for one minute (see nsk.share.runner.ThreadsRunner and35* nsk.share.runner.RunParams. It passes if no exceptions are generated.36*37* @library /vmTestbase38* /test/lib39* @run main/othervm gc.gctests.ThreadGC.ThreadGC -gp random(arrays) -ms low40*/4142package gc.gctests.ThreadGC;4344import nsk.share.gc.*;45import nsk.share.gc.gp.*;46import java.util.*;4748public class ThreadGC extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {49private GarbageProducer garbageProducer;50private MemoryStrategy memoryStrategy;51private Reclaimer reclaimer;52private int count;53private long size;5455private class Devourer implements Runnable {56private Object[] arr = null;57private int index;5859public void run() {60if (arr == null || index >= count) {61arr = null;62arr = new Object[count];63index = 0;64synchronized (reclaimer) {65reclaimer.notify();66}67}68arr[index] = garbageProducer.create(size);69++index;70}71}7273private class Reclaimer implements Runnable {74private long waitTime = 1000;7576public void run() {77try {78synchronized (this) {79this.wait(waitTime);80}81} catch (InterruptedException e) {82}83System.gc();84}85}8687protected Runnable createRunnable(int i) {88if (i == 0)89return new Devourer();90else if (i == 1) {91reclaimer = new Reclaimer();92return reclaimer;93} else94return null;95}9697public void run() {98size = GarbageUtils.getArraySize(runParams.getTestMemory(), memoryStrategy);99count = GarbageUtils.getArrayCount(runParams.getTestMemory(), memoryStrategy);100runParams.setIterations(count);101super.run();102}103104public static void main(String[] args) {105GC.runTest(new ThreadGC(), args);106}107108public void setGarbageProducer(GarbageProducer garbageProducer) {109this.garbageProducer = garbageProducer;110}111112public void setMemoryStrategy(MemoryStrategy memoryStrategy) {113this.memoryStrategy = memoryStrategy;114}115116}117118119