1 /*
2 * Copyright (c) 2018, 2025, 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 package jdk.jfr.event.oldobject;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import jdk.jfr.Recording;
30 import jdk.jfr.internal.test.WhiteBox;
31 import jdk.test.lib.jfr.EventNames;
32
33 /**
34 * @test
35 * @requires vm.flagless
36 * @requires vm.hasJFR
37 * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
38 * @library /test/lib /test/jdk
39 * @modules jdk.jfr/jdk.jfr.internal.test
40 * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestHeapShallow
41 */
42 public class TestHeapShallow {
43 private final static long LARGE_OBJECT_FACTOR = 509; // prime number
44
45 static class LeakObject {
46 Object object = null;
47
48 public LeakObject(Object object) {
49 this.object = object;
50 }
51 }
52
53 public static ArrayList<LeakObject> leak = new ArrayList<>(OldObjects.MIN_SIZE);
54
55 public static void main(String[] args) throws Exception {
56 WhiteBox.setWriteAllObjectSamples(true);
57
58 try (Recording recording = new Recording()) {
59 recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
60 recording.start();
61
62 addObjectsToShallowArray(leak);
63 recording.stop();
64 if (OldObjects.countMatchingEvents(recording, "addObjectsToShallowArray", byte[].class, "object", LeakObject.class, -1) < 1) {
65 throw new Exception("Could not find shallow leak object");
66 }
67 }
68 }
69
70 private static void addObjectsToShallowArray(List<LeakObject> list) {
71 for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
72 if (i % LARGE_OBJECT_FACTOR == 0) {
73 // Triggers allocation outside TLAB path
74 list.add(new LeakObject(new byte[16384]));
75 } else {
76 // Triggers allocation in TLAB path
77 list.add(new LeakObject(new byte[10]));
78 }
79 }
80 }
81 }