Skip to content

Commit 8154274

Browse files
committed
Add PointersBufferPoolMXBean to track allocated/deallocated pointers
1 parent 8a0b08d commit 8154274

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/main/java/org/bytedeco/javacpp/Pointer.java

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.ByteOrder;
3333
import java.util.Iterator;
3434
import java.util.concurrent.atomic.AtomicInteger;
35+
import java.util.concurrent.atomic.AtomicLong;
3536
import org.bytedeco.javacpp.annotation.Name;
3637
import org.bytedeco.javacpp.annotation.Platform;
3738
import org.bytedeco.javacpp.tools.Generator;
@@ -285,6 +286,7 @@ static class DeallocatorReference extends PhantomReference<Pointer> implements D
285286
Deallocator deallocator;
286287

287288
static volatile long totalBytes = 0;
289+
static AtomicLong totalCount = new AtomicLong();
288290
long bytes;
289291

290292
AtomicInteger count;
@@ -298,6 +300,7 @@ final void add() {
298300
next.prev = head = this;
299301
}
300302
totalBytes += bytes;
303+
totalCount.incrementAndGet();
301304
}
302305
}
303306

@@ -316,6 +319,7 @@ final void remove() {
316319
}
317320
prev = next = this;
318321
totalBytes -= bytes;
322+
totalCount.decrementAndGet();
319323
}
320324
}
321325

@@ -527,6 +531,11 @@ public static long totalBytes() {
527531
return DeallocatorReference.totalBytes;
528532
}
529533

534+
/** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */
535+
public static long totalCount() {
536+
return DeallocatorReference.totalCount.get();
537+
}
538+
530539
/** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */
531540
public static long maxPhysicalBytes() {
532541
return maxPhysicalBytes;

src/main/java/org/bytedeco/javacpp/tools/Logger.java

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public static Logger create(Class cls) {
4343
static {
4444
String s = System.getProperty("org.bytedeco.javacpp.logger.debug", "false").toLowerCase();
4545
debug = s.equals("true") || s.equals("t") || s.equals("");
46+
47+
String mx = System.getProperty("org.bytedeco.javacpp.mxbean", "false").toLowerCase();
48+
if (mx.equals("true") || mx.equals("t") || mx.equals("")) {
49+
PointersBufferPoolMXBean.register();
50+
}
4651
}
4752

4853
/** Returns the "org.bytedeco.javacpp.logger.debug" system property. */

0 commit comments

Comments
 (0)