Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/gc/compact/Compact.java
41159 views
/*1* Copyright (c) 2007, 2018, 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*/22package vm.gc.compact;2324import java.util.*;2526import java.util.concurrent.atomic.AtomicInteger;27import nsk.share.test.*;28import nsk.share.gc.*;29import nsk.share.gc.gp.*;3031/**32* Test garbage collector compaction.33*34* The test starts several threads which create objects using35* given garbage producer until OOM. The references are kept36* in an array. The references to even elements are cleared37* and objects of larger size are created until OOM. The garbage38* collector will have to compact free space to free memory for39* new objects.40*41* The size of the second half of created objects could be set explictly.42*43* This process is repeated.44*/45public class Compact extends ThreadedGCTest implements GarbageProducerAware, GarbageProducer1Aware, MemoryStrategyAware {4647private GarbageProducer garbageProducer;48private GarbageProducer garbageProducer1;49private MemoryStrategy memoryStrategy;50private long size;51private long size2;52private static long customSize = 0;53static AtomicInteger allocations = new AtomicInteger();5455private class Worker implements Runnable {5657private List<Object> bricks;58private ExecutionController stresser;5960public void run() {61if (stresser == null) {62stresser = getExecutionController();63}64try {65bricks = new ArrayList<Object>();66while (stresser.continueExecution()) {67bricks.add(garbageProducer.create(size));68}69} catch (OutOfMemoryError e) {70}71if (bricks == null) {72return;73}74int count = bricks.size();75for (int i = 0; stresser.continueExecution() && i < count; i += 2) {76bricks.set(i, null);77}78try {79for (int i = 0; stresser.continueExecution() && i < count; i += 2) {80bricks.set(i, garbageProducer1.create(size2));81allocations.incrementAndGet();82}83} catch (OutOfMemoryError e) {84}85bricks = null;86}87}8889public Runnable createRunnable(int i) {90return new Worker();91}9293public void run() {94size = memoryStrategy.getSize(runParams.getTestMemory());95size2 = customSize == 096? size2 = size * 297: customSize;98super.run();99}100101public final void setGarbageProducer(GarbageProducer garbageProducer) {102this.garbageProducer = garbageProducer;103}104105public final void setGarbageProducer1(GarbageProducer garbageProducer1) {106this.garbageProducer1 = garbageProducer1;107}108109public final void setMemoryStrategy(MemoryStrategy memoryStrategy) {110this.memoryStrategy = memoryStrategy;111}112113static void setHumongousSizeFromParams(String[] args) {114for (int i = 0; i < args.length; ++i) {115if (args[i].equals("-size2")) {116customSize = Long.parseLong(args[i + 1]);117return;118}119}120}121122public static void main(String[] args) {123setHumongousSizeFromParams(args);124GC.runTest(new Compact(), args);125}126}127128129