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.List;
27 
28 import jdk.jfr.Recording;
29 import jdk.jfr.consumer.RecordedEvent;
30 import jdk.jfr.internal.test.WhiteBox;
31 import jdk.test.lib.jfr.EventNames;
32 import jdk.test.lib.jfr.Events;
33 
34 /**
35  * @test
36  * @requires vm.flagless
37  * @requires vm.hasJFR
38  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
39  * @library /test/lib /test/jdk
40  * @modules jdk.jfr/jdk.jfr.internal.test
41  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestCircularReference
42  */
43 public class TestCircularReference {
44 
45     static class TestCircularReferrer {
46         // Allocate array to trigger sampling code path for interpreter / c1
47         final byte[] leak = new byte[1_0000_000];
48         TestCircularReferrer reference;
49 
50         public void setReference(TestCircularReferrer reference) {
51             this.reference = reference;
52         }
53     }
54 
55     public final static List<Object> referenceHolder = new ArrayList<>(OldObjects.MIN_SIZE);
56 
57     public static void main(String[] args) throws Exception {
58         WhiteBox.setWriteAllObjectSamples(true);
59 
60         try (Recording r = new Recording()) {
61             r.enable(EventNames.OldObjectSample).with("cutoff", "infinity").withStackTrace();
62             r.start();
63 
64             TestCircularReferrer a = new TestCircularReferrer();
65             TestCircularReferrer b = new TestCircularReferrer();
66             TestCircularReferrer c = new TestCircularReferrer();
67             a.setReference(b);
68             b.setReference(c);
69             c.setReference(a);
70             referenceHolder.add(a);
71 
72             TestCircularReferrer selfReflector = new TestCircularReferrer();
73             selfReflector.setReference(selfReflector);
74             referenceHolder.add(selfReflector);
75 
76             r.stop();
77 
78             List<RecordedEvent> events = Events.fromRecording(r);
79             if (events.isEmpty()) {
80                 throw new AssertionError("Expected Old Object Sample events!");
81             }
82         }
83     }
84 }