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