J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4076287 |
| 26 | * @summary Invoking get on a SoftReference shouldn't pin the referent |
| 27 | * @run main/othervm -ms16m -mx16m Pin |
| 28 | * @author Peter Jones |
| 29 | * @author Mark Reinhold |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | import java.lang.ref.SoftReference; |
| 34 | |
| 35 | public class Pin { |
| 36 | |
| 37 | final static int NUM_BLOCKS = 128; |
| 38 | final static int BLOCK_SIZE = 32768; |
| 39 | |
| 40 | public static void main(String[] args) throws Exception { |
| 41 | |
| 42 | SoftReference[] blocks = new SoftReference[NUM_BLOCKS]; |
| 43 | |
| 44 | byte[] block; |
| 45 | |
| 46 | System.err.println("Filling array with " + NUM_BLOCKS + |
| 47 | " SoftReferences to blocks of " + |
| 48 | BLOCK_SIZE + " bytes."); |
| 49 | |
| 50 | for (int i = 0; i < NUM_BLOCKS; ++ i) { |
| 51 | block = new byte[BLOCK_SIZE]; |
| 52 | SoftReference ref = new SoftReference(block); |
| 53 | blocks[i] = ref; |
| 54 | } |
| 55 | block = null; |
| 56 | |
| 57 | System.err.println("Allowing SoftReferences to be enqueued."); |
| 58 | System.gc(); |
| 59 | Thread.sleep(1000); |
| 60 | |
| 61 | /* -- Commenting out the following section will hide the bug -- */ |
| 62 | System.err.println("Invoking get() on SoftReferences."); |
| 63 | for (int i = 0; i < NUM_BLOCKS; ++ i) { |
| 64 | block = (byte[]) blocks[i].get(); |
| 65 | } |
| 66 | block = null; |
| 67 | /* -- end -- */ |
| 68 | |
| 69 | System.err.println("Forcing desperate garbage collection..."); |
| 70 | java.util.Vector chain = new java.util.Vector(); |
| 71 | try { |
| 72 | while (true) { |
| 73 | System.gc(); |
| 74 | int[] hungry = new int[65536]; |
| 75 | chain.addElement(hungry); |
| 76 | Thread.sleep(100); // yield, for what it's worth |
| 77 | } |
| 78 | } catch (OutOfMemoryError e) { |
| 79 | System.err.println("Got OutOfMemoryError, as expected."); |
| 80 | } |
| 81 | |
| 82 | int emptyCount = 0, fullCount = 0; |
| 83 | System.err.print("Examining contents of array:"); |
| 84 | for (int i = 0; i < NUM_BLOCKS; ++ i) { |
| 85 | block = (byte[]) blocks[i].get(); |
| 86 | if (block == null) { |
| 87 | emptyCount++; |
| 88 | } else { |
| 89 | fullCount++; |
| 90 | } |
| 91 | } |
| 92 | System.err.println(" " + emptyCount + " empty, " + |
| 93 | fullCount + " full."); |
| 94 | if (emptyCount == 0) |
| 95 | throw new Exception("No SoftReference instances were cleared"); |
| 96 | } |
| 97 | |
| 98 | } |