Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/clearHandleTable/ClearHandleTable.java
38828 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;3233public class ClearHandleTable {34public static void main(String[] args) throws Exception {35final int nreps = 100;36ObjectOutputStream oout =37new ObjectOutputStream(new ByteArrayOutputStream());38WeakReference[] refs = new WeakReference[nreps];3940for (int i = 0; i < nreps; i++) {41String str = new String("blargh");42oout.writeObject(str);43refs[i] = new WeakReference(str);44}4546oout.reset();47exhaustMemory();4849for (int i = 0; i < nreps; i++) {50if (refs[i].get() != null) {51throw new Error("failed to garbage collect object " + i);52}53}54}5556static void exhaustMemory() {57ArrayList blob = new ArrayList();58try {59for (;;) {60blob.add(new int[0xFFFF]);61}62} catch (OutOfMemoryError e) {63}64}65}666768