Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/SoftReference/Pin.java
38828 views
/*1* Copyright (c) 1997, 2005, 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/* @test24* @bug 407628725* @summary Invoking get on a SoftReference shouldn't pin the referent26* @run main/othervm -ms16m -mx16m Pin27* @author Peter Jones28* @author Mark Reinhold29*/303132import java.lang.ref.SoftReference;3334public class Pin {3536final static int NUM_BLOCKS = 128;37final static int BLOCK_SIZE = 32768;3839public static void main(String[] args) throws Exception {4041SoftReference[] blocks = new SoftReference[NUM_BLOCKS];4243byte[] block;4445System.err.println("Filling array with " + NUM_BLOCKS +46" SoftReferences to blocks of " +47BLOCK_SIZE + " bytes.");4849for (int i = 0; i < NUM_BLOCKS; ++ i) {50block = new byte[BLOCK_SIZE];51SoftReference ref = new SoftReference(block);52blocks[i] = ref;53}54block = null;5556System.err.println("Allowing SoftReferences to be enqueued.");57System.gc();58Thread.sleep(1000);5960/* -- Commenting out the following section will hide the bug -- */61System.err.println("Invoking get() on SoftReferences.");62for (int i = 0; i < NUM_BLOCKS; ++ i) {63block = (byte[]) blocks[i].get();64}65block = null;66/* -- end -- */6768System.err.println("Forcing desperate garbage collection...");69java.util.Vector chain = new java.util.Vector();70try {71while (true) {72System.gc();73int[] hungry = new int[65536];74chain.addElement(hungry);75Thread.sleep(100); // yield, for what it's worth76}77} catch (OutOfMemoryError e) {78chain = null; // Free memory for further work.79System.err.println("Got OutOfMemoryError, as expected.");80}8182int emptyCount = 0, fullCount = 0;83System.err.print("Examining contents of array:");84for (int i = 0; i < NUM_BLOCKS; ++ i) {85block = (byte[]) blocks[i].get();86if (block == null) {87emptyCount++;88} else {89fullCount++;90}91}92System.err.println(" " + emptyCount + " empty, " +93fullCount + " full.");94if (emptyCount == 0)95throw new Exception("No SoftReference instances were cleared");96}9798}99100101