1
1
/*
2
- * Copyright (c) 2005, 2013 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2005, 2022 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -202,6 +202,7 @@ static class VMSupport {
202
202
private static final boolean CHURN_AVAILABLE ;
203
203
private static NotificationListener listener ;
204
204
private static Multiset <String > churn ;
205
+ private static boolean started ;
205
206
206
207
static {
207
208
ALLOC_AVAILABLE = tryInitAlloc ();
@@ -239,7 +240,8 @@ private static boolean tryInitChurn() {
239
240
throw new UnsupportedOperationException ("GarbageCollectorMXBean cannot notify" );
240
241
}
241
242
}
242
- newListener ();
243
+ churn = new HashMultiset <>();
244
+ listener = newListener ();
243
245
return true ;
244
246
} catch (Throwable e ) {
245
247
System .out .println ("Churn profiling is not available: " + e .getMessage ());
@@ -257,7 +259,6 @@ private static long[] getAllocatedBytes(long[] threadIds) {
257
259
}
258
260
259
261
private static NotificationListener newListener () {
260
- churn = new HashMultiset <>();
261
262
try {
262
263
final Class <?> infoKlass = Class .forName ("com.sun.management.GarbageCollectionNotificationInfo" );
263
264
final Field notifNameField = infoKlass .getField ("GARBAGE_COLLECTION_NOTIFICATION" );
@@ -304,10 +305,11 @@ public static HotspotAllocationSnapshot getSnapshot() {
304
305
305
306
public static synchronized void startChurnProfile () {
306
307
if (!CHURN_AVAILABLE ) return ;
307
- if (listener != null ) {
308
+ if (started ) {
308
309
throw new IllegalStateException ("Churn profile already started" );
309
310
}
310
- listener = newListener ();
311
+ started = true ;
312
+ churn = new HashMultiset <>();
311
313
try {
312
314
for (GarbageCollectorMXBean bean : ManagementFactory .getGarbageCollectorMXBeans ()) {
313
315
((NotificationEmitter ) bean ).addNotificationListener (listener , null , null );
@@ -319,7 +321,7 @@ public static synchronized void startChurnProfile() {
319
321
320
322
public static synchronized void finishChurnProfile () {
321
323
if (!CHURN_AVAILABLE ) return ;
322
- if (listener == null ) {
324
+ if (! started ) {
323
325
throw new IllegalStateException ("Churn profile already stopped" );
324
326
}
325
327
@@ -337,7 +339,7 @@ public static synchronized void finishChurnProfile() {
337
339
// Do nothing
338
340
}
339
341
}
340
- listener = null ;
342
+ started = false ;
341
343
}
342
344
343
345
public static synchronized Multiset <String > getChurn () {
0 commit comments