@@ -55,28 +55,34 @@ public class GCInspector implements NotificationListener, GCInspectorMXBean
5555
5656 /*
5757 * The field from java.nio.Bits that tracks the total number of allocated
58- * bytes of direct memory requires via ByteBuffer.allocateDirect that have not been GCed.
58+ * bytes of direct memory requested via ByteBuffer.allocateDirect that have not been GCed.
5959 */
6060 final static Field BITS_TOTAL_CAPACITY_JAVA_8 ;
6161 final static Field BITS_TOTAL_CAPACITY_JAVA_11 ;
6262
6363 static
6464 {
65- Field totalTempFieldJava8 = null ;
66- Field totalTempFieldJava11 = null ;
65+ Class <?> bitsClass = null ;
66+
6767 try
6868 {
69- Class <?> bitsClass = Class .forName ("java.nio.Bits" );
70- totalTempFieldJava8 = getField (bitsClass , "totalCapacity" );
71- totalTempFieldJava11 = getField (bitsClass , "TOTAL_CAPACITY" );
69+ bitsClass = Class .forName ("java.nio.Bits" );
7270 }
7371 catch (Throwable t )
7472 {
75- logger .debug ("Error accessing field of java.nio.Bits" , t );
76- //Don't care, will just return the dummy value -1 if we can't get at the field in this JVM
73+ logger .debug ("Error returning class of java.nio.Bits" , t );
74+ }
75+
76+ if (bitsClass != null )
77+ {
78+ BITS_TOTAL_CAPACITY_JAVA_8 = getField (bitsClass , "totalCapacity" );
79+ BITS_TOTAL_CAPACITY_JAVA_11 = getField (bitsClass , "TOTAL_CAPACITY" );
80+ }
81+ else
82+ {
83+ BITS_TOTAL_CAPACITY_JAVA_8 = null ;
84+ BITS_TOTAL_CAPACITY_JAVA_11 = null ;
7785 }
78- BITS_TOTAL_CAPACITY_JAVA_8 = totalTempFieldJava8 ;
79- BITS_TOTAL_CAPACITY_JAVA_11 = totalTempFieldJava11 ;
8086 }
8187
8288 static final class State
@@ -342,7 +348,11 @@ private static Field getField(Class<?> clazz, String fieldName)
342348 }
343349
344350 /**
345- * This method works well with JDK 8/11
351+ * Retrieves the value of a Field, handling both regular long fields and AtomicLong fields.
352+ *
353+ * @param field the Field to retrieve the value from
354+ * @param isAtomicLong true if the field is an AtomicLong, false if it's a regular long
355+ * @return the field value, or -1 if retrieval fails or field is null.
346356 */
347357 private static long getFieldValue (Field field , boolean isAtomicLong )
348358 {
0 commit comments