Pin.java revision 12745:f068a4ffddd2
155714Skris/* 255714Skris * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 355714Skris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 455714Skris * 555714Skris * This code is free software; you can redistribute it and/or modify it 655714Skris * under the terms of the GNU General Public License version 2 only, as 755714Skris * published by the Free Software Foundation. 855714Skris * 955714Skris * This code is distributed in the hope that it will be useful, but WITHOUT 1055714Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1155714Skris * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1255714Skris * version 2 for more details (a copy is included in the LICENSE file that 1355714Skris * accompanied this code). 1455714Skris * 1555714Skris * You should have received a copy of the GNU General Public License version 1655714Skris * 2 along with this work; if not, write to the Free Software Foundation, 1755714Skris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1855714Skris * 1955714Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2055714Skris * or visit www.oracle.com if you need additional information or have any 2155714Skris * questions. 2255714Skris */ 2355714Skris 2455714Skris/* @test 2555714Skris * @bug 4076287 2655714Skris * @summary Invoking get on a SoftReference shouldn't pin the referent 2755714Skris * @run main/othervm -ms16m -mx16m Pin 2855714Skris * @author Peter Jones 2955714Skris * @author Mark Reinhold 3055714Skris */ 3155714Skris 3255714Skris 3355714Skrisimport java.lang.ref.SoftReference; 3455714Skris 3555714Skrispublic class Pin { 3655714Skris 3755714Skris static final int NUM_BLOCKS = 128; 3855714Skris static final int BLOCK_SIZE = 32768; 3955714Skris 4055714Skris public static void main(String[] args) throws Exception { 4155714Skris 4255714Skris SoftReference[] blocks = new SoftReference[NUM_BLOCKS]; 4355714Skris 4455714Skris byte[] block; 4555714Skris 4655714Skris System.err.println("Filling array with " + NUM_BLOCKS + 4755714Skris " SoftReferences to blocks of " + 4855714Skris BLOCK_SIZE + " bytes."); 4955714Skris 5055714Skris for (int i = 0; i < NUM_BLOCKS; ++ i) { 5155714Skris block = new byte[BLOCK_SIZE]; 5255714Skris SoftReference ref = new SoftReference(block); 5355714Skris blocks[i] = ref; 5455714Skris } 5555714Skris block = null; 5655714Skris 5755714Skris System.err.println("Allowing SoftReferences to be enqueued."); 5855714Skris System.gc(); 59160814Ssimon Thread.sleep(1000); 60160814Ssimon 61160814Ssimon /* -- Commenting out the following section will hide the bug -- */ 62160814Ssimon System.err.println("Invoking get() on SoftReferences."); 63160814Ssimon for (int i = 0; i < NUM_BLOCKS; ++ i) { 64160814Ssimon block = (byte[]) blocks[i].get(); 65160814Ssimon } 66109998Smarkm block = null; 6759191Skris /* -- end -- */ 6855714Skris 6955714Skris System.err.println("Forcing desperate garbage collection..."); 7055714Skris java.util.Vector chain = new java.util.Vector(); 7155714Skris try { 7255714Skris while (true) { 7355714Skris System.gc(); 7455714Skris int[] hungry = new int[65536]; 7555714Skris chain.addElement(hungry); 7655714Skris Thread.sleep(100); // yield, for what it's worth 7755714Skris } 7855714Skris } catch (OutOfMemoryError e) { 7955714Skris chain = null; // Free memory for further work. 8055714Skris System.err.println("Got OutOfMemoryError, as expected."); 8155714Skris } 8255714Skris 8359191Skris int emptyCount = 0, fullCount = 0; 8455714Skris System.err.print("Examining contents of array:"); 8555714Skris for (int i = 0; i < NUM_BLOCKS; ++ i) { 8655714Skris block = (byte[]) blocks[i].get(); 8755714Skris if (block == null) { 8855714Skris emptyCount++; 8955714Skris } else { 9055714Skris fullCount++; 9155714Skris } 92160814Ssimon } 93160814Ssimon System.err.println(" " + emptyCount + " empty, " + 94160814Ssimon fullCount + " full."); 9555714Skris if (emptyCount == 0) 9655714Skris throw new Exception("No SoftReference instances were cleared"); 97160814Ssimon } 9859191Skris 99160814Ssimon} 100160814Ssimon