Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/FinalizeTest05/FinalizeTest05.java
41159 views
/*1* Copyright (c) 2009, 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/FinalizeTest05.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.FinalizeTest05.FinalizeTest0533*/3435package gc.gctests.FinalizeTest05;3637import nsk.share.gc.*;38import java.util.*;39import nsk.share.TestFailure;40import nsk.share.test.ExecutionController;41import nsk.share.test.Stresser;4243/**44*/45public class FinalizeTest05 extends GCTestBase {4647private final int allocRatio = 5;48private final int size = 1024 * 2;49private int count = 1000;50private ExecutionController stresser;5152private void runOne() {53ArrayList objs = new ArrayList(count);54Object o;55for (int i = 0; i < count; i++) {56if (i % allocRatio == 0) {57o = new FinMemoryObject(size);58objs.add(o);59} else {60o = new byte[size - Memory.getObjectExtraSize()];61}62}63FinMemoryObject.dumpStatistics();64o = null;6566/* force finalization */67Algorithms.eatMemory(stresser);68System.gc();69Runtime.getRuntime().runFinalization();70System.gc();71Runtime.getRuntime().runFinalization();7273FinMemoryObject.dumpStatistics();7475boolean error;76System.out.println("Allocated: " + FinMemoryObject.getAllocatedCount());77System.out.println("Finalized: " + FinMemoryObject.getFinalizedCount());78error = (FinMemoryObject.getFinalizedCount() != 0);7980// Just hit the objs array to do say VM that we use this object81// and it should be alive in this point.82objs.clear();83if (error) {84throw new TestFailure("Test failed.");85}86}8788public void run() {89stresser = new Stresser(runParams.getStressOptions());90stresser.start(runParams.getIterations());91count = (int) Math.min(runParams.getTestMemory() / size, Integer.MAX_VALUE);92System.out.println("Allocating " + count93+ " objects. 1 out of " + allocRatio94+ " will have a finalizer.");95System.out.flush();96runOne();97}9899public static void main(String[] args) {100GC.runTest(new FinalizeTest05(), args);101}102}103104105