本文整理匯總了Java中java.lang.management.MemoryPoolMXBean.getUsage方法的典型用法代碼示例。如果您正苦於以下問題:Java MemoryPoolMXBean.getUsage方法的具體用法?Java MemoryPoolMXBean.getUsage怎麽用?Java MemoryPoolMXBean.getUsage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.management.MemoryPoolMXBean
的用法示例。
在下文中一共展示了MemoryPoolMXBean.getUsage方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: reportUsage
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
private void reportUsage(MemoryPoolMXBean mxBean, MetricRecorder.Context metricContext)
{
String name = mxBean.getName();
Metric usedMetric = Metric.define("MemoryPoolUsed_" + name);
Metric maxMetric = Metric.define("MemoryPoolMax_" + name);
Metric percMetric = Metric.define("MemoryPoolUsage_" + name);
MemoryUsage usage = mxBean.getUsage();
long used = usage.getUsed();
long max = usage.getMax();
metricContext.record(usedMetric, used / M, Unit.MEGABYTE);
// max can be undefined (-1) https://docs.oracle.com/javase/8/docs/api/java/lang/management/MemoryUsage.html
if (max >= 0) {
metricContext.record(maxMetric, max / M, Unit.MEGABYTE);
double used_percent = 100.0 * ((double)used/(double)max);
metricContext.record(percMetric, used_percent, Unit.PERCENT);
}
}
示例2: poolSummaries
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
/**
* Returns a summary information about the memory pools.
*/
public static String poolSummaries() {
// Why ? list-archive?4273859
// How ? http://stackoverflow.com/questions/697336/how-do-i-programmatically-find-out-my-permgen-space-usage
// http://stackoverflow.com/questions/8356416/xxmaxpermsize-with-or-without-xxpermsize
StringBuilder sb = new StringBuilder();
Iterator<MemoryPoolMXBean> iter =
ManagementFactory.getMemoryPoolMXBeans().iterator();
while (iter.hasNext()) {
MemoryPoolMXBean item = iter.next();
String name = item.getName();
MemoryType type = item.getType();
MemoryUsage usage = item.getUsage();
MemoryUsage peak = item.getPeakUsage();
MemoryUsage collections = item.getCollectionUsage();
sb.append(String.format("Memory pool name: " + name
+ ", type: " + type
+ ", usage: " + usage
+ ", peak: " + peak
+ ", collections: " + collections
+ "\n"));
}
return sb.toString();
}
示例3: eatMetaspace
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
private List<Object> eatMetaspace(float targetUsage) {
List<Object> list = new ArrayList<>();
MemoryPoolMXBean metaspacePool = getMatchedMemoryPool(".*Metaspace.*");
float currentUsage;
GeneratedClassProducer gp = new GeneratedClassProducer();
do {
try {
list.add(gp.create(0));
} catch (OutOfMemoryError oome) {
list = null;
throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace.");
}
MemoryUsage memoryUsage = metaspacePool.getUsage();
currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax());
} while (currentUsage < targetUsage);
return list;
}
示例4: getMemoryPool
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
@Override
public MemoryPool getMemoryPool(final MemoryPoolMXBean memoryPoolBean) {
final MemoryPool.Type type = MemoryPool.Type.from(memoryPoolBean);
boolean found = false;
for (final MemoryPool.Type supportedType : getSupportedMemoryPoolTypes()) {
if (type == supportedType) {
found = true;
break;
}
}
if (!found) {
throw new IllegalStateException(
String.format("The memory pool type '%s' is not supported on the JVM type '%s'",
type, this));
}
return new MemoryPoolImpl(type, new Supplier<MemoryUsage>() {
@Override
public MemoryUsage get() {
return memoryPoolBean.getUsage();
}
});
}
示例5: verifyMemoryPool
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
private static void verifyMemoryPool(MemoryPoolMXBean pool, boolean isMaxDefined) {
MemoryUsage mu = pool.getUsage();
assertDefined(mu.getInit(), "init");
assertDefined(mu.getUsed(), "used");
assertDefined(mu.getCommitted(), "committed");
if (isMaxDefined) {
assertDefined(mu.getMax(), "max");
} else {
assertUndefined(mu.getMax(), "max");
}
}
示例6: getUsage
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
/**
* Get MemoryUsage from MemoryPoolMXBean which name matches passed string.
*
* @param name
* @return MemoryUsage
*/
private static MemoryUsage getUsage(String name){
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
if (pool.getName().matches(name)) {
return pool.getUsage();
}
}
return null;
}
示例7: getOldGenAllocationRatio
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
/**
* Returns allocation rate for old gen based on appropriate MemoryPoolMXBean
* memory usage.
*
* @return allocation rate
*/
public float getOldGenAllocationRatio() {
MemoryPoolMXBean oldGenBean = getMatchedMemoryPool(".*Old.*|.*Tenured.*");
MemoryUsage usage = oldGenBean.getUsage();
System.out.format("Memory usage for %1s.\n", oldGenBean.getName());
System.out.format("Used: %1d\n", usage.getUsed());
System.out.format("Commited: %1d\n", usage.getCommitted());
System.out.format("Max: %1d\n", usage.getMax());
return ((float) usage.getUsed()) / usage.getCommitted();
}
示例8: jvmStats
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
public static JvmStats jvmStats() {
MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
long heapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
long heapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
long heapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();
memUsage = memoryMXBean.getNonHeapMemoryUsage();
long nonHeapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
long nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
List<MemoryPool> pools = new ArrayList<>();
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
try {
MemoryUsage usage = memoryPoolMXBean.getUsage();
MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
if (name == null) { // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
continue;
}
pools.add(new MemoryPool(name,
usage.getUsed() < 0 ? 0 : usage.getUsed(),
usage.getMax() < 0 ? 0 : usage.getMax(),
peakUsage.getUsed() < 0 ? 0 : peakUsage.getUsed(),
peakUsage.getMax() < 0 ? 0 : peakUsage.getMax()
));
} catch (Exception ex) {
/* ignore some JVMs might barf here with:
* java.lang.InternalError: Memory Pool not found
* we just omit the pool in that case!*/
}
}
Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
Threads threads = new Threads(threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount());
List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
GarbageCollector[] collectors = new GarbageCollector[gcMxBeans.size()];
for (int i = 0; i < collectors.length; i++) {
GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
collectors[i] = new GarbageCollector(GcNames.getByGcName(gcMxBean.getName(), gcMxBean.getName()),
gcMxBean.getCollectionCount(), gcMxBean.getCollectionTime());
}
GarbageCollectors garbageCollectors = new GarbageCollectors(collectors);
List<BufferPool> bufferPoolsList = Collections.emptyList();
try {
List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
bufferPoolsList = new ArrayList<>(bufferPools.size());
for (BufferPoolMXBean bufferPool : bufferPools) {
bufferPoolsList.add(new BufferPool(bufferPool.getName(), bufferPool.getCount(),
bufferPool.getTotalCapacity(), bufferPool.getMemoryUsed()));
}
} catch (Exception e) {
// buffer pools are not available
}
Classes classes = new Classes(classLoadingMXBean.getLoadedClassCount(), classLoadingMXBean.getTotalLoadedClassCount(),
classLoadingMXBean.getUnloadedClassCount());
return new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime(), mem, threads,
garbageCollectors, bufferPoolsList, classes);
}
示例9: format
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
String format(LogParams logParams, int n)
{
if(n == 0)
{
String csMessage = logParams.toString();
csMessage = logParams.toString();
StackTraceElement stackElem = logParams.m_caller;
String csOut;
if(stackElem != null)
{
String csFile = stackElem.getFileName();
int nLine = stackElem.getLineNumber();
csOut = csFile+"("+nLine+"):"+csMessage;
}
else
{
csOut = "():"+csMessage;
}
csOut += "\r\n******** Mem:";
int nMem = 0;
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p: pools)
{
if(p.getType().compareTo(MemoryType.HEAP) == 0)
{
String cs = p.getName();
MemoryUsage mem = p.getUsage();
long l = mem.getUsed();
long lOldMem = m_mem[nMem];
long lOffset = l - lOldMem;
csOut += cs+"="+l+"["+lOffset+"];";
m_mem[nMem] = l;
nMem++;
}
}
csOut += "\r\n";
return csOut;
}
return null;
}
示例10: readHeapPoolUsage
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
public static Map<String, Long> readHeapPoolUsage(List<MemoryPoolMXBean> pmbList) {
Map<String, Long> m = new LinkedHashMap<String, Long>();
for (MemoryPoolMXBean mpmb : pmbList) {
String jvmMemPoolName = getHeapPoolName(mpmb.getName().trim());
MemoryUsage mu = mpmb.getUsage();
m.put(jvmMemPoolName + "_use", mu.getUsed());
m.put(jvmMemPoolName + "_commit", mu.getCommitted());
m.put(jvmMemPoolName + "_max", mu.getMax());
m.put(jvmMemPoolName + "_init", mu.getInit());
}
return m;
}
示例11: readHeapPoolUsage
import java.lang.management.MemoryPoolMXBean; //導入方法依賴的package包/類
private void readHeapPoolUsage(MonitorElementInstance instance) {
List<MemoryPoolMXBean> pmbList = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean mpmb : pmbList) {
String jvmMemPoolName = getHeapPoolName(mpmb.getName().trim());
MemoryUsage mu = mpmb.getUsage();
instance.setValue(jvmMemPoolName + "_use", mu.getUsed());
instance.setValue(jvmMemPoolName + "_commit", mu.getCommitted());
instance.setValue(jvmMemPoolName + "_max", mu.getMax());
instance.setValue(jvmMemPoolName + "_init", mu.getInit());
}
}