Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/weak003/weak003.java
41161 views
/*1* Copyright (c) 2007, 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/WeakReference/weak003.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.WeakReference.weak003.weak003 -t 133*/3435package gc.gctests.WeakReference.weak003;3637import java.lang.ref.Reference;38import nsk.share.test.*;39import nsk.share.TestFailure;40import nsk.share.gc.*;41import java.lang.ref.WeakReference;4243/**44* Test that GC clears weak references before throwing OOM.45*46* This test creates a number of weak references, then provokes47* GC with Algorithms.eatMemory and checks that all references48* have been cleared.49*50* This assertion is not clearly stated in javadoc for WeakReference,51* (but for SoftReference), but one would expect that quality GC52* will do it.53*/54public class weak003 extends ThreadedGCTest {5556class Worker implements Runnable {5758private int arrayLength;59private int objectSize = 100;60private Reference<MemoryObject>[] references;6162public void run() {63for (int i = 0; i < arrayLength; ++i) {64references[i] = new WeakReference<MemoryObject>(new MemoryObject(LocalRandom.nextInt(objectSize)));65}66Algorithms.eatMemory(getExecutionController());67if (!getExecutionController().continueExecution()) {68return;69}70// Check that all references have been cleared71int n = 0;72for (int i = 0; i < arrayLength; ++i) {73if (references[i].get() != null) {74++n;75}76}77if (n != 0) {78references = null;79throw new TestFailure("Some of the references have been not cleared: " + n);80}81}8283public void Worker() {84arrayLength = Memory.getArrayLength(runParams.getTestMemory(), Memory.getReferenceSize() + objectSize);85System.out.println("Array size: " + arrayLength);86references = new WeakReference[arrayLength];87}88}8990@Override91protected Runnable createRunnable(int i) {92return new Worker();93}9495public static void main(String[] args) {96GC.runTest(new weak003(), args);97}98}99100101