Path: blob/master/test/jdk/java/io/Serializable/clearHandleTable/ClearHandleTable.java
41153 views
/*1* Copyright (c) 2000, 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 433218425* @summary Ensure that ObjectOutputStream properly releases strong references26* to written objects when reset() is called.27*/2829import java.io.*;30import java.lang.ref.WeakReference;31import java.util.ArrayList;32import java.util.Iterator;33import java.util.List;3435public class ClearHandleTable {36private static final int TIMES = 1000;3738public static void main(String[] args) throws Exception {39final int nreps = 100;40ObjectOutputStream oout =41new ObjectOutputStream(new ByteArrayOutputStream());42List<WeakReference<?>> refs = new ArrayList<>(nreps);4344for (int i = 0; i < nreps; i++) {45String str = new String("blargh");46oout.writeObject(str);47refs.add(new WeakReference<Object>(str));48}4950oout.reset();5152int count = 0;53for (int i=0; i<TIMES; i++) {54// relying on, possibly multiple, System.gc calls to clear weak references55System.gc();5657Iterator<WeakReference<?>> itr = refs.iterator();58while(itr.hasNext()) {59WeakReference<?> ref = itr.next();60if (ref.get() == null) {61itr.remove();62}63}64if (refs.isEmpty())65break;66Thread.sleep(20);67count++;68if (count % 10 == 0)69System.out.println("Looping " + count + " times");70}7172if (!refs.isEmpty()) {73throw new Error("failed to garbage collect object");74}75}76}777879