System.gc() causes for jcl (BufferPoolMXBean)

Bernd Eckenfels ecki at zusammenkunft.net
Fri Jun 19 06:00:15 UTC 2015


Hello,

Am Sat, 6 Jun 2015 19:07:07 +0200
schrieb Bernd Eckenfels <ecki at zusammenkunft.net>:

> - why is BufferPoolMXBean not tracking allocation failures, allocation
>   count, alignment flag and maximum size (maybe even modifyable).
> Would it be worth to contribute something in this area or is that
> supposed to be covered by some events (and if yes, when will they be
> exposed to JMX).

I had a look at it, the following patch would track number of
allocations for direct and mapped buffers as well as number of "failed"
allocations for the direct buffers. (The name of the JMX Attributes
need to be changed, what would you suggest?)

diff -r bc4bbb07768e src/java.base/share/classes/java/nio/Bits.java
--- a/src/java.base/share/classes/java/nio/Bits.java	Thu Jun 18
17:20:42 2015 -0700 +++
b/src/java.base/share/classes/java/nio/Bits.java	Fri Jun 19
05:55:35 2015 +0000 @@ -602,6 +602,8 @@ private static final AtomicLong
reservedMemory = new AtomicLong(); private static final AtomicLong
totalCapacity = new AtomicLong(); private static final AtomicLong count
= new AtomicLong();
+    private static final AtomicLong totalAllocations = new
AtomicLong();
+    private static final AtomicLong failedAllocations = new
AtomicLong(); private static volatile boolean memoryLimitSet = false;
     // max. number of sleeps during try-reserving with exponentially
     // increasing delay before throwing OutOfMemoryError:
@@ -624,6 +626,8 @@
             return;
         }
 
+        failedAllocations.incrementAndGet();
+
         final JavaLangRefAccess jlra =
SharedSecrets.getJavaLangRefAccess(); 
         // retry while helping enqueue pending Reference objects
@@ -683,6 +687,7 @@
             if (totalCapacity.compareAndSet(totalCap, totalCap + cap))
{ reservedMemory.addAndGet(size);
                 count.incrementAndGet();
+                totalAllocations.incrementAndGet();
                 return true;
             }
         }
@@ -723,6 +728,13 @@
                         public long getMemoryUsed() {
                             return Bits.reservedMemory.get();
                         }
+                        @Override
+                        public long getTotalAllocatedBuffers() {
+                            return Bits.totalAllocations.get();
+                        }
+                        public long getFailedAllocatedBuffers() {
+                            return Bits.failedAllocations.get();
+                        }
                     };
                 }
                 @Override
diff -r bc4bbb07768e
src/java.base/share/classes/sun/misc/JavaNioAccess.java ---
a/src/java.base/share/classes/sun/misc/JavaNioAccess.java	Thu
Jun 18 17:20:42 2015 -0700 +++
b/src/java.base/share/classes/sun/misc/JavaNioAccess.java	Fri
Jun 19 05:55:35 2015 +0000 @@ -37,6 +37,8 @@ long getCount(); long
getTotalCapacity(); long getMemoryUsed();
+        long getTotalAllocatedBuffers();
+        long getFailedAllocatedBuffers();
     }
     BufferPool getDirectBufferPool();
 
diff -r bc4bbb07768e
src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java ---
a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
Thu Jun 18 17:20:42 2015 -0700 +++
b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
Fri Jun 19 05:55:35 2015 +0000 @@ -797,6 +797,7 @@ static volatile int
count; static volatile long totalSize; static volatile long
totalCapacity;
+        static volatile long totalAllocated;
 
         private volatile long address;
         private final long size;
@@ -816,6 +817,7 @@
                 count++;
                 totalSize += size;
                 totalCapacity += cap;
+                totalAllocated++;
             }
         }
 
@@ -993,6 +995,14 @@
             public long getMemoryUsed() {
                 return Unmapper.totalSize;
             }
+            @Override
+            public long getTotalAllocatedBuffers() {
+                return Unmapper.totalAllocated;
+            }
+            @Override
+            public long getFailedAllocatedBuffers() {
+                return -1;
+            }
         };
     }
 
diff -r bc4bbb07768e
src/java.management/share/classes/java/lang/management/BufferPoolMXBean.java
---
a/src/java.management/share/classes/java/lang/management/BufferPoolMXBean.java
Thu Jun 18 17:20:42 2015 -0700 +++
b/src/java.management/share/classes/java/lang/management/BufferPoolMXBean.java
Fri Jun 19 05:55:35 2015 +0000 @@ -90,4 +90,25 @@
      *          the memory usage is not available
      */
     long getMemoryUsed();
+
+    /**
+     * Returns an estimate of the total number of buffer allocations
since start of JVM.
+     *
+     * @return Number of direct buffers (estimated) allocated since
JVM start.
+     */
+    long getTotalAllocatedBuffers();
+
+    /**
+     * Returns an estimate of the number of times the buffer limit was
reached.
+     * <p>
+     * This counts all allocations which do not immediatelly suceed.
The counter
+     * gets increased no matter if the retried allocation failed or
caused an
+     * {@link java.lang.OutOfMemoryException}.
+     *
+     * @return Number of allocations which failed or have been retried
or {@code -1L} 
+     *         if no limit is enforced. This especially tracks {@code
-XX:MaxDirectMemory}
+     *         limit for {@link java.nio.ByteBuffer#allocateDirect
direct} buffers.
+     */
+    long getFailedAllocatedBuffers();
+     
 }
diff -r bc4bbb07768e
src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
---
a/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
Thu Jun 18 17:20:42 2015 -0700 +++
b/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
Fri Jun 19 05:55:35 2015 +0000 @@ -257,6 +257,14 @@ public long
getMemoryUsed() { return pool.getMemoryUsed(); }
+            @Override
+            public long getTotalAllocatedBuffers() {
+                return pool.getTotalAllocatedBuffers();
+            }
+            @Override
+            public long getFailedAllocatedBuffers() {
+                return pool.getFailedAllocatedBuffers();
+            }
         };
     }
 
Gruss
Bernd



More information about the hotspot-gc-dev mailing list