1 /*
 2  * Copyright (c) 2015, 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 package jdk.jfr.event.oldobject;
24 
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Random;
29 import java.util.Vector;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.atomic.AtomicInteger;
32 
33 import jdk.jfr.Recording;
34 import jdk.jfr.consumer.RecordedClass;
35 import jdk.jfr.consumer.RecordedEvent;
36 import jdk.jfr.consumer.RecordedFrame;
37 import jdk.jfr.consumer.RecordedMethod;
38 import jdk.jfr.consumer.RecordedObject;
39 import jdk.jfr.consumer.RecordedStackTrace;
40 import jdk.jfr.internal.test.WhiteBox;
41 import jdk.test.lib.jfr.EventNames;
42 import jdk.test.lib.jfr.Events;
43 
44 /**
45  * @test
46  * @requires vm.flagless
47  * @requires vm.hasJFR
48  * @library /test/lib /test/jdk
49  * @modules jdk.jfr/jdk.jfr.internal.test
50  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestLargeRootSet
51  */
52 public class TestLargeRootSet {
53     static class Node {
54         Node left;
55         Node right;
56         Object value;
57     }
58 
59     static class Leak {
60         // Leaking object has to be of some size,
61         // otherwise Node object wins most of the
62         // slots in the object queue.
63         // In a normal application, objects would
64         // be of various size and allocated over a
65         // longer period of time. This would create
66         // randomness not present in the test.
67         public long value1;
68         public Object value2;
69         float value3;
70         int value4;
71         double value5;
72     }
73 
74     public static void main(String[] args) throws Exception {
75         WhiteBox.setWriteAllObjectSamples(true);
76         WhiteBox.setSkipBFS(true);
77         HashMap<Object, Node> leaks = new HashMap<>();
78         try (Recording r = new Recording()) {
79             r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
80             r.start();
81             for (int i = 0; i < 1_000_000; i++) {
82                 Node node = new Node();
83                 node.left = new Node();
84                 node.right = new Node();
85                 node.right.value = new Leak();
86                 leaks.put(i, node);
87             }
88             r.stop();
89             List<RecordedEvent> events = Events.fromRecording(r);
90             Events.hasEvents(events);
91             for (RecordedEvent e : events) {
92                 RecordedClass type = e.getValue("object.type");
93                 if (type.getName().equals(Leak.class.getName())) {
94                     return;
95                 }
96             }
97         }
98     }
99 }