Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/io/ObjectStreamClass/TestOSCClassLoaderLeak.java
66644 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.lang.ref.WeakReference;
25
import java.lang.reflect.Constructor;
26
import java.io.ByteArrayOutputStream;
27
import java.io.InputStream;
28
import java.io.ObjectStreamClass;
29
import java.io.ObjectStreamField;
30
import java.io.Serializable;
31
import java.util.Arrays;
32
import org.testng.annotations.Test;
33
import static org.testng.Assert.assertNotNull;
34
import static org.testng.Assert.assertTrue;
35
36
import jdk.test.lib.util.ForceGC;
37
38
/* @test
39
* @bug 8277072
40
* @library /test/lib/
41
* @build jdk.test.lib.util.ForceGC
42
* @summary ObjectStreamClass caches keep ClassLoaders alive
43
* @run testng TestOSCClassLoaderLeak
44
*/
45
public class TestOSCClassLoaderLeak {
46
47
@Test
48
public void testClassLoaderLeak() throws Exception {
49
TestClassLoader myOwnClassLoader = new TestClassLoader();
50
Class<?> loadClass = myOwnClassLoader.loadClass("ObjectStreamClass_MemoryLeakExample");
51
Constructor con = loadClass.getConstructor();
52
con.setAccessible(true);
53
Object objectStreamClass_MemoryLeakExample = con.newInstance();
54
objectStreamClass_MemoryLeakExample.toString();
55
56
WeakReference<Object> myOwnClassLoaderWeakReference = new WeakReference<>(myOwnClassLoader);
57
assertNotNull(myOwnClassLoaderWeakReference.get());
58
objectStreamClass_MemoryLeakExample = null;
59
myOwnClassLoader = null;
60
loadClass = null;
61
con = null;
62
assertNotNull(myOwnClassLoaderWeakReference.get());
63
64
ForceGC gc = new ForceGC();
65
assertTrue(gc.await(() -> myOwnClassLoaderWeakReference.get() == null));
66
}
67
}
68
69
class ObjectStreamClass_MemoryLeakExample {
70
private static final ObjectStreamField[] fields = ObjectStreamClass.lookup(TestClass.class).getFields();
71
public ObjectStreamClass_MemoryLeakExample() {
72
}
73
74
@Override
75
public String toString() {
76
return Arrays.toString(fields);
77
}
78
}
79
80
class TestClassLoader extends ClassLoader {
81
82
@Override
83
public Class<?> loadClass(String name) throws ClassNotFoundException {
84
if (name.equals("TestClass") || name.equals("ObjectStreamClass_MemoryLeakExample")) {
85
byte[] bt = loadClassData(name);
86
return defineClass(name, bt, 0, bt.length);
87
} else {
88
return super.loadClass(name);
89
}
90
}
91
92
private static byte[] loadClassData(String className) {
93
ByteArrayOutputStream byteSt = new ByteArrayOutputStream();
94
try (InputStream is = TestClassLoader.class.getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")) {
95
int len = 0;
96
while ((len = is.read()) != -1) {
97
byteSt.write(len);
98
}
99
} catch (java.io.IOException e) {
100
e.printStackTrace();
101
}
102
return byteSt.toByteArray();
103
}
104
}
105
106
class TestClass implements Serializable {
107
public String x;
108
}
109
110