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.consumer.RecordedEvent;
 31 import jdk.jfr.internal.test.WhiteBox;
 32 import jdk.test.lib.jfr.EventNames;
 33 import jdk.test.lib.jfr.Events;
 34 
 35 /**
 36  * @test
 37  * @requires vm.flagless
 38  * @requires vm.hasJFR
 39  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
 40  * @library /test/lib /test/jdk
 41  * @modules jdk.jfr/jdk.jfr.internal.test
 42  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestListenerLeak
 43  */
 44 public class TestListenerLeak {
 45 
 46     private interface TestListener {
 47         void onListen();
 48     }
 49 
 50     static class Stuff {
 51     }
 52 
 53     static class ListenerThread extends Thread {
 54 
 55         private List<Stuff[]> stuff;
 56 
 57         public ListenerThread(List<Stuff[]> stuff) {
 58             this.stuff = stuff;
 59         }
 60 
 61         public void run() {
 62             listener.add(new TestListener() {
 63                 @Override
 64                 public void onListen() {
 65                     System.out.println(stuff);
 66                 }
 67             });
 68         }
 69     }
 70 
 71     private static List<TestListener> listener = new ArrayList<>();
 72 
 73     public static void main(String[] args) throws Exception {
 74         WhiteBox.setWriteAllObjectSamples(true);
 75         while (true) {
 76             try (Recording r = new Recording()) {
 77                 r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
 78                 r.start();
 79                 listenerLeak();
 80                 r.stop();
 81                 List<RecordedEvent> events = Events.fromRecording(r);
 82                 if (OldObjects.countMatchingEvents(events, Stuff[].class, null, null, -1, "listenerLeak") != 0) {
 83                     return; // Success
 84                 }
 85                 System.out.println("Could not find leak with " + Stuff[].class + ". Retrying.");
 86             }
 87         }
 88     }
 89 
 90     private static void listenerLeak() throws InterruptedException {
 91         List<Stuff[]> stuff = new ArrayList<>(OldObjects.MIN_SIZE);
 92         for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
 93             // Allocate array to trigger sampling code path for interpreter / c1
 94             stuff.add(new Stuff[0]);
 95         }
 96 
 97         ListenerThread t = new ListenerThread(stuff);
 98         t.start();
 99         t.join();
100     }
101 
102 }