jdk-24/test/jdk/java/io/ObjectStreamClass/ObjectStreamClassCaching.java
Stefan Karlsson d20034b09c 8307058: Implementation of Generational ZGC
Co-authored-by: Stefan Karlsson <stefank@openjdk.org>
Co-authored-by: Erik Österlund <eosterlund@openjdk.org>
Co-authored-by: Axel Boldt-Christmas <aboldtch@openjdk.org>
Co-authored-by: Per Liden <pliden@openjdk.org>
Co-authored-by: Stefan Johansson <sjohanss@openjdk.org>
Co-authored-by: Albert Mingkun Yang <ayang@openjdk.org>
Co-authored-by: Erik Helin <ehelin@openjdk.org>
Co-authored-by: Roberto Castañeda Lozano <rcastanedalo@openjdk.org>
Co-authored-by: Nils Eliasson <neliasso@openjdk.org>
Co-authored-by: Martin Doerr <mdoerr@openjdk.org>
Co-authored-by: Leslie Zhai <lzhai@openjdk.org>
Co-authored-by: Fei Yang <fyang@openjdk.org>
Co-authored-by: Yadong Wang <yadongwang@openjdk.org>
Reviewed-by: eosterlund, aboldtch, rcastanedalo
2023-05-11 13:59:37 +00:00

138 lines
4.6 KiB
Java

/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.ArrayList;
import org.testng.annotations.Test;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
/*
* @test id=G1
* @requires vm.gc.G1
* @bug 8277072
* @library /test/lib/
* @summary ObjectStreamClass caches keep ClassLoaders alive (G1 GC)
* @run testng/othervm -Xmx64m -XX:+UseG1GC ObjectStreamClassCaching
*/
/*
* @test id=Parallel
* @requires vm.gc.Parallel
* @bug 8277072
* @library /test/lib/
* @summary ObjectStreamClass caches keep ClassLoaders alive (Parallel GC)
* @run testng/othervm -Xmx64m -XX:+UseParallelGC ObjectStreamClassCaching
*/
/*
* Disabled for ZGC Generational.
* TODO: Find correct appropriate solution to the flakiness of this test.
*/
/*
* @test id=Shenandoah
* @requires vm.gc.Shenandoah
* @bug 8277072
* @library /test/lib/
* @summary ObjectStreamClass caches keep ClassLoaders alive (Shenandoah GC)
* @run testng/othervm -Xmx64m -XX:+UseShenandoahGC ObjectStreamClassCaching
*/
/*
* @test id=Serial
* @requires vm.gc.Serial
* @bug 8277072
* @library /test/lib/
* @summary ObjectStreamClass caches keep ClassLoaders alive (Serial GC)
* @run testng/othervm -Xmx64m -XX:+UseSerialGC ObjectStreamClassCaching
*/
public class ObjectStreamClassCaching {
/**
* Test methods execute in same VM and are ordered by name.
* We test effectiveness 1st which is sensitive to previous allocations when ZGC is used.
*/
@Test
public void test1CacheEffectiveness() throws Exception {
var list = new ArrayList<>();
var ref1 = lookupObjectStreamClass(TestClass1.class);
var ref2 = newWeakRef();
boolean oome = false;
try {
while (!ref2.refersTo(null)) {
list.add(new byte[1024 * 1024 * 1]); // 1 MiB chunks
System.out.println("1MiB allocated...");
Thread.sleep(5L);
}
} catch (OutOfMemoryError e) {
// release
list = null;
oome = true;
}
assertFalse(oome, "WeakReference was not cleared although memory was pressed hard");
assertFalse(ref1.refersTo(null),
"Cache lost entry together with WeakReference being cleared although memory was not under pressure");
System.gc();
Thread.sleep(100L);
}
@Test
public void test2CacheReleaseUnderMemoryPressure() throws Exception {
var list = new ArrayList<>();
var ref = lookupObjectStreamClass(TestClass2.class);
try {
while (!ref.refersTo(null)) {
list.add(new byte[1024 * 1024 * 4]); // 4 MiB chunks
System.out.println("4MiB allocated...");
}
} catch (OutOfMemoryError e) {
// release
list = null;
}
System.gc();
Thread.sleep(100L);
assertTrue(ref.refersTo(null),
"Cache still has entry although memory was pressed hard");
}
// separate method so that the looked-up ObjectStreamClass is not kept on stack
private static Reference<?> lookupObjectStreamClass(Class<?> cl) {
return new WeakReference<>(ObjectStreamClass.lookup(cl));
}
// separate method so that the new Object() is not kept on stack
private static Reference<?> newWeakRef() {
return new WeakReference<>(new Object());
}
static class TestClass1 implements Serializable {
}
static class TestClass2 implements Serializable {
}
}