Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/WeakReferenceTest/WeakReferenceTest.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*/22package gc.gctests.WeakReference;2324import java.util.Random;25import java.util.ArrayList;26import java.lang.ref.WeakReference;27import nsk.share.TestFailure;28import nsk.share.gc.GC;29import nsk.share.gc.GCTestBase;3031/**32* Original description from JRockit test:33*34* Tests for the WeakReference handling in JRockit.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 WeakReferenceTest extends GCTestBase {4142/**43* Test that all WeakReferences has been cleared after44* the GC has run. This test should not throw an OOME45* during the test execution.46*47*/48@Override49public void run() {50long seed;51int minSize;52int maxSize;53double memPercentToFill;54long totalMemAlloced = 0;55long memToAlloc = 0;56Runtime r = Runtime.getRuntime();5758seed = runParams.getSeed();59minSize = 2048;60maxSize = 32768;6162memPercentToFill = 0.45;6364memToAlloc = (long) (r.maxMemory() * memPercentToFill);6566Random rndGenerator = new Random(seed);67long multiplier = maxSize - minSize;68ArrayList arrWeakRefs = new ArrayList();6970try {71while (totalMemAlloced < memToAlloc) {72int allocationSize = ((int) (rndGenerator.nextDouble()73* multiplier)) + minSize;74byte[] tmp = new byte[allocationSize];7576arrWeakRefs.add(new WeakReference(tmp));77totalMemAlloced += allocationSize;7879// Make sure the temporary object is dereferenced80tmp = null;81}8283System.gc();8485long numberOfNotNulledObjects = 0;8687for (int i = 0; i < arrWeakRefs.size(); i++) {88WeakReference wr = (WeakReference) arrWeakRefs.get(i);89Object o = wr.get();9091if (o != null) {92numberOfNotNulledObjects++;93}94}9596if (numberOfNotNulledObjects > 0) {97throw new TestFailure(numberOfNotNulledObjects + " out of "98+ arrWeakRefs.size() + " WeakReferences was not "99+ "null after the GC had run");100}101102log.info("All WeakReferences was cleared after the "103+ "GC had run");104} catch (OutOfMemoryError oome) {105throw new TestFailure("OutOfMemoryException was thrown. This should "106+ "not happen during the execution of this test.");107} finally {108arrWeakRefs = null;109}110}111112public static void main(String[] args) {113GC.runTest(new WeakReferenceTest(), args);114}115}116117118