下面列出了java.lang.management.MemoryType#HEAP 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @return Pair of global memstore size and memory type(ie. on heap or off heap).
*/
public static Pair<Long, MemoryType> getGlobalMemStoreSize(Configuration conf) {
long offheapMSGlobal = conf.getLong(OFFHEAP_MEMSTORE_SIZE_KEY, 0);// Size in MBs
if (offheapMSGlobal > 0) {
// Off heap memstore size has not relevance when MSLAB is turned OFF. We will go with making
// this entire size split into Chunks and pooling them in MemstoreLABPoool. We dont want to
// create so many on demand off heap chunks. In fact when this off heap size is configured, we
// will go with 100% of this size as the pool size
if (MemStoreLAB.isEnabled(conf)) {
// We are in offheap Memstore use
long globalMemStoreLimit = (long) (offheapMSGlobal * 1024 * 1024); // Size in bytes
return new Pair<>(globalMemStoreLimit, MemoryType.NON_HEAP);
} else {
// Off heap max memstore size is configured with turning off MSLAB. It makes no sense. Do a
// warn log and go with on heap memstore percentage. By default it will be 40% of Xmx
LOG.warn("There is no relevance of configuring '" + OFFHEAP_MEMSTORE_SIZE_KEY + "' when '"
+ MemStoreLAB.USEMSLAB_KEY + "' is turned off."
+ " Going with on heap global memstore size ('" + MEMSTORE_SIZE_KEY + "')");
}
}
return new Pair<>(getOnheapGlobalMemStoreSize(conf), MemoryType.HEAP);
}
/**
* collect the pool division of java
*/
protected Map<String, Object> getPoolDivision() {
if (LOG.isDebugEnabled()) {
LOG.debug("==> RangerJVMMetricUtil.getPoolDivision()");
}
Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mpBean.getType() == MemoryType.HEAP) {
poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== RangerJVMMetricUtil.getPoolDivision()" + poolDivisionValues);
}
return poolDivisionValues;
}
/**
* Determines if the name of the memory pool MXBean provided matches a list of
* known young generation pool names.
*
* @param memoryPoolMXBean
* The memory pool MXBean to check.
* @return True if the pool name matches a known young generation pool name,
* false otherwise.
*/
static boolean isEden(MemoryPoolMXBean memoryPoolMXBean) {
if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
return false;
}
String name = memoryPoolMXBean.getName();
return name.equals("Par Eden Space") // Oracle ParNew with Concurrent Mark Sweep GC
|| name.equals("PS Eden Space") // Oracle Parallel GC
|| name.equals("G1 Eden") // Oracle G1 GC
//|| name.equals("Nursery") // BEA JRockit 1.5, 1.6 GC
|| name.equals("Eden Space") // Hitachi 1.5 GC
// Allow an unknown pool name to monitor
|| (HEAP_EDEN_POOL != null && name.equals(HEAP_EDEN_POOL));
}
private void registerForNotifications() {
// Register the listener with MemoryMXBean
NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
emitter.addNotificationListener(listener, null, null);
// set collection usage threshold.
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
if (pool.getType() == MemoryType.HEAP &&
pool.isUsageThresholdSupported() &&
pool.isCollectionUsageThresholdSupported()) {
long threshold = (pool.getUsage().getMax() * thresholdPercentage) / 100;
logger.info("setting collection threshold for " + pool.getName() +
" with max " + pool.getUsage().getMax() +
" to " + threshold);
pool.setCollectionUsageThreshold(threshold);
monitoredPools.put(pool.getName(), pool.getCollectionUsageThresholdCount());
} else {
logger.info("skip monitoring for pool " + pool.getName());
}
}
}
/**
* Return true if we're above the low watermark
*/
public FlushType isAboveLowWaterMark() {
// for onheap memstore we check if the global memstore size and the
// global heap overhead is greater than the global memstore lower mark limit
if (memType == MemoryType.HEAP) {
if (getGlobalMemStoreHeapSize() >= globalMemStoreLimitLowMark) {
return FlushType.ABOVE_ONHEAP_LOWER_MARK;
}
} else {
if (getGlobalMemStoreOffHeapSize() >= globalMemStoreLimitLowMark) {
// Indicates that the offheap memstore's size is greater than the global memstore
// lower limit
return FlushType.ABOVE_OFFHEAP_LOWER_MARK;
} else if (getGlobalMemStoreHeapSize() >= globalOnHeapMemstoreLimitLowMark) {
// Indicates that the offheap memstore's heap overhead is greater than the global memstore
// onheap lower limit
return FlushType.ABOVE_ONHEAP_LOWER_MARK;
}
}
return FlushType.NORMAL;
}
private MemoryUsage getMemoryUsage(MemoryType type) {
if (type == MemoryType.HEAP) {
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
} else {
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
}
public MemoryType getType() {
if (isHeap) {
return MemoryType.HEAP;
} else {
return MemoryType.NON_HEAP;
}
}
public MemoryType getType() {
if (isHeap) {
return MemoryType.HEAP;
} else {
return MemoryType.NON_HEAP;
}
}
private MemoryUsage getMemoryUsage(MemoryType type) {
if (type == MemoryType.HEAP) {
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
} else {
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
}
void setGlobalMemStoreLimits(long newGlobalMemstoreLimit) {
if (this.memType == MemoryType.HEAP) {
this.globalMemStoreLimit = newGlobalMemstoreLimit;
this.globalMemStoreLimitLowMark =
(long) (this.globalMemStoreLimit * this.globalMemStoreLimitLowMarkPercent);
} else {
this.globalOnHeapMemstoreLimit = newGlobalMemstoreLimit;
this.globalOnHeapMemstoreLimitLowMark =
(long) (this.globalOnHeapMemstoreLimit * this.globalMemStoreLimitLowMarkPercent);
}
}
private MemoryUsage getMemoryUsage(MemoryType type) {
if (type == MemoryType.HEAP) {
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
} else {
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
}
private static MemoryPoolMXBean findTenuredGenPool() {
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
return pool;
}
}
throw new AssertionError("could not find tenured space");
}
/**
* collect the pool division of java
*/
private static void pooldivision(Map<String, Object> memory) {
Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mpBean.getType() == MemoryType.HEAP) {
poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
}
}
memory.put("memory_pool_usages", poolDivisionValues);
}
/**
* Tenured Space Pool can be determined by it being of type HEAP and by it
* being possible to set the usage threshold.
*
* @return MXBean for the Tenured Space Pool
*/
private static MemoryPoolMXBean findTenuredGenPool() {
// I don't know whether this approach is better, or whether
// we should rather check for the pool name "Tenured Gen"?
for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans())
if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
return pool;
}
throw new AssertionError("Could not find tenured space");
}
private MemoryUsage getMemoryUsage(MemoryType type) {
if (type == MemoryType.HEAP) {
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
} else {
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
}
/**
* Determines if the name of the memory pool MXBean provided matches a list of
* known survivor space pool names.
*
* @param memoryPoolMXBean
* The memory pool MXBean to check.
* @return True if the pool name matches a known survivor space pool name,
* false otherwise.
*/
static boolean isSurvivor(MemoryPoolMXBean memoryPoolMXBean) {
if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
return false;
}
String name = memoryPoolMXBean.getName();
return name.equals("Par Survivor Space") // Oracle Concurrent Mark Sweep GC
|| name.equals("PS Survivor Space") // Oracle Parallel GC
|| name.equals("G1 Survivor") // Oracle G1 GC
|| name.equals("Survivor Space") // Hitachi 1.5 GC
// Allow an unknown pool name to monitor
|| (HEAP_SURVIVOR_POOL != null && name.equals(HEAP_SURVIVOR_POOL));
}
/**
* Tenured Space Pool can be determined by it being of type HEAP and by it
* being possible to set the usage threshold.
*/
private static MemoryPoolMXBean findTenuredGenPool() {
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
// I don't know whether this approach is better, or whether
// we should rather check for the pool name "Tenured Gen"?
if (pool.getType() == MemoryType.HEAP
&& pool.isUsageThresholdSupported()) {
return pool;
}
}
throw new AssertionError("Could not find tenured space");
}
public MemoryType getType() {
if (isHeap) {
return MemoryType.HEAP;
} else {
return MemoryType.NON_HEAP;
}
}
private MemoryUsage getMemoryUsage(MemoryType type) {
if (type == MemoryType.HEAP) {
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
} else {
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
}
}
public MemoryType getType() {
if (isHeap) {
return MemoryType.HEAP;
} else {
return MemoryType.NON_HEAP;
}
}