-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add
PointerBufferPoolMXBean
to track allocations and deallocatio…
…ns of `Pointer` (pull #413)
- Loading branch information
1 parent
52951ca
commit 341f65c
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/bytedeco/javacpp/tools/PointerBufferPoolMXBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.bytedeco.javacpp.tools; | ||
|
||
import org.bytedeco.javacpp.Pointer; | ||
|
||
import javax.management.*; | ||
import java.lang.management.BufferPoolMXBean; | ||
import java.lang.management.ManagementFactory; | ||
|
||
public class PointerBufferPoolMXBean implements BufferPoolMXBean { | ||
|
||
private static final Logger LOGGER = Logger.create(PointerBufferPoolMXBean.class); | ||
private static final String JAVACPP_MXBEAN_NAME = "javacpp"; | ||
private static final ObjectName OBJECT_NAME; | ||
|
||
static { | ||
ObjectName objectName = null; | ||
try { | ||
objectName = new ObjectName("java.nio:type=BufferPool,name=" + JAVACPP_MXBEAN_NAME); | ||
} catch (MalformedObjectNameException e) { | ||
LOGGER.warn("Could not create OBJECT_NAME for " + JAVACPP_MXBEAN_NAME); | ||
} | ||
OBJECT_NAME = objectName; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return JAVACPP_MXBEAN_NAME; | ||
} | ||
|
||
@Override | ||
public ObjectName getObjectName() { | ||
return OBJECT_NAME; | ||
} | ||
|
||
@Override | ||
public long getCount() { | ||
return Pointer.totalCount(); | ||
} | ||
|
||
@Override | ||
public long getTotalCapacity() { | ||
return Pointer.maxPhysicalBytes(); | ||
} | ||
|
||
@Override | ||
public long getMemoryUsed() { | ||
return Pointer.totalBytes(); | ||
} | ||
|
||
public static void register() { | ||
if (OBJECT_NAME != null) { | ||
try { | ||
ManagementFactory.getPlatformMBeanServer().registerMBean(new PointerBufferPoolMXBean(), OBJECT_NAME); | ||
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) { | ||
LOGGER.warn("Could not register " + JAVACPP_MXBEAN_NAME + " BufferPoolMXBean"); | ||
} | ||
} | ||
} | ||
} |