Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/WeakReferenceEvilTest/WeakReferenceEvilTest.java
41161 views
/*1* Copyright (c) 2004, 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*/2223package gc.gctests.WeakReference;2425import java.util.Random;26import java.util.ArrayList;27import java.lang.ref.WeakReference;28import nsk.share.TestFailure;29import nsk.share.gc.GC;30import nsk.share.gc.GCTestBase;3132/**33* Original description from JRockit test:34* Tests for the WeakReference handling in JRockit (in a more evil way :)).35*36* This test must be run with a mx value set, as well as with37* -XXfullsystemgc, to ensure Runtime.maxMemory() doesn't return 038* and that System.gc() gets run when called from Java.39*/40public class WeakReferenceEvilTest extends GCTestBase {4142/**43* Test the WeakReference handling in a more evil way. In this test,44* it will only keep every Xth object, thus causing more fragmentation45* and fill the heap with unused objects. This test should not46* throw any OOME during the test execution.47*48* @return success if all WeakReferences are NULL after49* the test has run50*/51public void run() {52long seed;53int minSize;54int maxSize;55int keepEveryXthObject;56double memPercentToFill;57long counter = 0;58long totalMemAlloced = 0;59long memToAlloc = 0;60Runtime r = Runtime.getRuntime();6162seed = runParams.getSeed();63minSize = 2048;64maxSize = 32768;65memPercentToFill = 0.45;66keepEveryXthObject = 5;6768memToAlloc = (long) (r.maxMemory() * memPercentToFill);6970Random rndGenerator = new Random(seed);71long multiplier = maxSize - minSize;72ArrayList arrWeakRefs = new ArrayList();7374try {75while (totalMemAlloced < memToAlloc) {76int allocationSize = ((int) (rndGenerator.nextDouble()77* multiplier)) + minSize;78byte[] tmp = new byte[allocationSize];7980if (counter % keepEveryXthObject == 0) {81arrWeakRefs.add(new WeakReference(tmp));82totalMemAlloced += allocationSize;83}8485// Make sure the temporary object is dereferenced86tmp = null;8788counter++;89if (counter == Long.MAX_VALUE) {90counter = 0;91}92}9394System.gc();9596long numberOfNotNulledObjects = 0;9798for (int i = 0; i < arrWeakRefs.size(); i++) {99WeakReference wr = (WeakReference) arrWeakRefs.get(i);100Object o = wr.get();101102if (o != null) {103numberOfNotNulledObjects++;104}105}106107if (numberOfNotNulledObjects > 0) {108throw new TestFailure(numberOfNotNulledObjects + " out of "109+ arrWeakRefs.size() + " WeakReferences was not "110+ "null after the GC had run");111}112113log.info("All WeakReferences was cleared after the "114+ "GC had run");115} catch (OutOfMemoryError oome) {116throw new TestFailure("OutOfMemoryException was thrown. This should "117+ "not happen during the execution of this test.");118} finally {119arrWeakRefs = null;120}121}122public static void main(String[] args) {123GC.runTest(new WeakReferenceEvilTest(), args);124}125}126127128