Path: blob/master/test/jdk/java/io/ObjectStreamClass/TestOSCClassLoaderLeak.java
66644 views
/*1* Copyright (c) 2021, 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*/2223import java.lang.ref.WeakReference;24import java.lang.reflect.Constructor;25import java.io.ByteArrayOutputStream;26import java.io.InputStream;27import java.io.ObjectStreamClass;28import java.io.ObjectStreamField;29import java.io.Serializable;30import java.util.Arrays;31import org.testng.annotations.Test;32import static org.testng.Assert.assertNotNull;33import static org.testng.Assert.assertTrue;3435import jdk.test.lib.util.ForceGC;3637/* @test38* @bug 827707239* @library /test/lib/40* @build jdk.test.lib.util.ForceGC41* @summary ObjectStreamClass caches keep ClassLoaders alive42* @run testng TestOSCClassLoaderLeak43*/44public class TestOSCClassLoaderLeak {4546@Test47public void testClassLoaderLeak() throws Exception {48TestClassLoader myOwnClassLoader = new TestClassLoader();49Class<?> loadClass = myOwnClassLoader.loadClass("ObjectStreamClass_MemoryLeakExample");50Constructor con = loadClass.getConstructor();51con.setAccessible(true);52Object objectStreamClass_MemoryLeakExample = con.newInstance();53objectStreamClass_MemoryLeakExample.toString();5455WeakReference<Object> myOwnClassLoaderWeakReference = new WeakReference<>(myOwnClassLoader);56assertNotNull(myOwnClassLoaderWeakReference.get());57objectStreamClass_MemoryLeakExample = null;58myOwnClassLoader = null;59loadClass = null;60con = null;61assertNotNull(myOwnClassLoaderWeakReference.get());6263ForceGC gc = new ForceGC();64assertTrue(gc.await(() -> myOwnClassLoaderWeakReference.get() == null));65}66}6768class ObjectStreamClass_MemoryLeakExample {69private static final ObjectStreamField[] fields = ObjectStreamClass.lookup(TestClass.class).getFields();70public ObjectStreamClass_MemoryLeakExample() {71}7273@Override74public String toString() {75return Arrays.toString(fields);76}77}7879class TestClassLoader extends ClassLoader {8081@Override82public Class<?> loadClass(String name) throws ClassNotFoundException {83if (name.equals("TestClass") || name.equals("ObjectStreamClass_MemoryLeakExample")) {84byte[] bt = loadClassData(name);85return defineClass(name, bt, 0, bt.length);86} else {87return super.loadClass(name);88}89}9091private static byte[] loadClassData(String className) {92ByteArrayOutputStream byteSt = new ByteArrayOutputStream();93try (InputStream is = TestClassLoader.class.getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")) {94int len = 0;95while ((len = is.read()) != -1) {96byteSt.write(len);97}98} catch (java.io.IOException e) {99e.printStackTrace();100}101return byteSt.toByteArray();102}103}104105class TestClass implements Serializable {106public String x;107}108109110