1 /*
   2  * Copyright (c) 2018, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.event.oldobject;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.jfr.internal.test.WhiteBox;
  34 import jdk.testlibrary.jfr.EventNames;
  35 import jdk.testlibrary.jfr.Events;
  36 import jdk.testlibrary.jfr.OldObjects;
  37 
  38 /*
  39  * @test
  40  * @key jfr
  41  * @requires vm.gc == "null"
  42  * @library /lib/testlibrary
  43  * @modules jdk.jfr/jdk.jfr.internal.test
  44  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestListenerLeak
  45  */
  46 public class TestListenerLeak {
  47 
  48     private interface TestListener {
  49         void onListen();
  50     }
  51 
  52     static class Stuff {
  53     }
  54 
  55     static class ListenerThread extends Thread {
  56 
  57         private List<Stuff[]> stuff;
  58 
  59         public ListenerThread(List<Stuff[]> stuff) {
  60             this.stuff = stuff;
  61         }
  62 
  63         public void run() {
  64             listener.add(new TestListener() {
  65                 @Override
  66                 public void onListen() {
  67                     System.out.println(stuff);
  68                 }
  69             });
  70         }
  71     }
  72 
  73     private static List<TestListener> listener = new ArrayList<>();
  74 
  75     public static void main(String[] args) throws Exception {
  76         WhiteBox.setWriteAllObjectSamples(true);
  77 
  78         try (Recording r = new Recording()) {
  79             r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
  80             r.start();
  81             listenerLeak();
  82             r.stop();
  83             List<RecordedEvent> events = Events.fromRecording(r);
  84             System.out.println("Event: " + events.size());
  85             for (RecordedEvent event : events) {
  86                 System.out.println("Event: " + event);
  87             }
  88             if (OldObjects.countMatchingEvents(events, Stuff[].class, null, null, -1, "listenerLeak") == 0) {
  89                 throw new Exception("Could not find leak with " + Stuff[].class);
  90             }
  91         }
  92     }
  93 
  94     private static void listenerLeak() throws InterruptedException {
  95         List<Stuff[]> stuff = new ArrayList<>(OldObjects.MIN_SIZE);
  96         for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
  97             // Allocate array to trigger sampling code path for interpreter / c1
  98             stuff.add(new Stuff[0]);
  99         }
 100 
 101         ListenerThread t = new ListenerThread(stuff);
 102         t.start();
 103         t.join();
 104     }
 105 
 106 }