Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/io/ObjectStreamClass/ObjectStreamClassCaching.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.Reference;
25
import java.lang.ref.WeakReference;
26
import java.io.ObjectStreamClass;
27
import java.io.Serializable;
28
import java.util.ArrayList;
29
import org.testng.annotations.Test;
30
import static org.testng.Assert.assertFalse;
31
import static org.testng.Assert.assertTrue;
32
33
/* @test
34
* @bug 8277072
35
* @library /test/lib/
36
* @summary ObjectStreamClass caches keep ClassLoaders alive
37
* @run testng/othervm -Xmx10m -XX:SoftRefLRUPolicyMSPerMB=1 ObjectStreamClassCaching
38
*/
39
public class ObjectStreamClassCaching {
40
41
@Test
42
public void testCachingEffectiveness() throws Exception {
43
var ref = lookupObjectStreamClass(TestClass.class);
44
System.gc();
45
Thread.sleep(100L);
46
// to trigger any ReferenceQueue processing...
47
lookupObjectStreamClass(AnotherTestClass.class);
48
assertFalse(ref.refersTo(null),
49
"Cache lost entry although memory was not under pressure");
50
}
51
52
@Test
53
public void testCacheReleaseUnderMemoryPressure() throws Exception {
54
var ref = lookupObjectStreamClass(TestClass.class);
55
pressMemoryHard(ref);
56
System.gc();
57
Thread.sleep(100L);
58
assertTrue(ref.refersTo(null),
59
"Cache still has entry although memory was pressed hard");
60
}
61
62
// separate method so that the looked-up ObjectStreamClass is not kept on stack
63
private static WeakReference<?> lookupObjectStreamClass(Class<?> cl) {
64
return new WeakReference<>(ObjectStreamClass.lookup(cl));
65
}
66
67
private static void pressMemoryHard(Reference<?> ref) {
68
try {
69
var list = new ArrayList<>();
70
while (!ref.refersTo(null)) {
71
list.add(new byte[1024 * 1024 * 64]); // 64 MiB chunks
72
}
73
} catch (OutOfMemoryError e) {
74
// release
75
}
76
}
77
}
78
79
class TestClass implements Serializable {
80
}
81
82
class AnotherTestClass implements Serializable {
83
}
84
85