use of java.lang.management.MemoryPoolMXBean in project Mindroid.java by Himmele.
the class ConsoleService method getMemoryUsage.
private static String getMemoryUsage() {
StringBuilder builder = new StringBuilder();
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage totalHeapMemoryUsage = memoryBean.getHeapMemoryUsage();
MemoryUsage totalNonHeapMemoryUsage = memoryBean.getNonHeapMemoryUsage();
builder.append("Summary:").append(System.lineSeparator());
builder.append("Heap memory: ");
addMemoryUsageSummary(builder, totalHeapMemoryUsage);
builder.append(System.lineSeparator());
builder.append("Non-heap memory: ");
addMemoryUsageSummary(builder, totalNonHeapMemoryUsage);
builder.append(System.lineSeparator()).append(System.lineSeparator());
List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
List<MemoryPoolMXBean> heapMemoryPools = new ArrayList<>();
List<MemoryPoolMXBean> nonHeapMemoryPools = new ArrayList<>();
for (MemoryPoolMXBean pool : memoryPools) {
if (pool.isValid()) {
if (pool.getType() == MemoryType.HEAP) {
heapMemoryPools.add(pool);
} else {
nonHeapMemoryPools.add(pool);
}
}
}
builder.append("Heap memory:").append(System.lineSeparator());
builder.append("Total heap memory:");
addMemoryUsage(builder, totalHeapMemoryUsage);
builder.append(System.lineSeparator());
for (MemoryPoolMXBean pool : heapMemoryPools) {
addMemoryPool(builder, pool);
builder.append(System.lineSeparator());
}
builder.append(System.lineSeparator()).append("Non-heap memory:").append(System.lineSeparator());
builder.append("Total non-heap memory:");
addMemoryUsage(builder, totalNonHeapMemoryUsage);
builder.append(System.lineSeparator());
for (MemoryPoolMXBean pool : nonHeapMemoryPools) {
addMemoryPool(builder, pool);
builder.append(System.lineSeparator());
}
List<BufferPoolMXBean> buffers = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
builder.append(System.lineSeparator()).append("Buffers:").append(System.lineSeparator());
for (BufferPoolMXBean buffer : buffers) {
builder.append(buffer.getName()).append(":");
builder.append(" count ").append(buffer.getCount());
builder.append(" memory ").append(buffer.getMemoryUsed()).append("B");
builder.append(System.lineSeparator());
}
return builder.toString();
}
use of java.lang.management.MemoryPoolMXBean in project siddhi by wso2.
the class ObjectSizeCalculator method getEffectiveMemoryLayoutSpecification.
@VisibleForTesting
static MemoryLayoutSpecification getEffectiveMemoryLayoutSpecification() {
final String vmName = System.getProperty("java.vm.name");
if (vmName == null || !(vmName.startsWith("Java HotSpot(TM) ") || vmName.startsWith("OpenJDK") || vmName.startsWith("TwitterJDK"))) {
throw new UnsupportedOperationException("ObjectSizeCalculator only supported on HotSpot VM");
}
final String dataModel = System.getProperty("sun.arch.data.model");
if ("32".equals(dataModel)) {
// Running with 32-bit data model.
return new MemoryLayoutSpecification() {
@Override
public int getArrayHeaderSize() {
return 12;
}
@Override
public int getObjectHeaderSize() {
return 8;
}
@Override
public int getObjectPadding() {
return 8;
}
@Override
public int getReferenceSize() {
return 4;
}
@Override
public int getSuperclassFieldPadding() {
return 4;
}
};
} else if (!"64".equals(dataModel)) {
throw new UnsupportedOperationException("Unrecognized value '" + dataModel + "' of sun.arch.data.model system property");
}
final String strVmVersion = System.getProperty("java.vm.version");
final int vmVersion = Integer.parseInt(strVmVersion.substring(0, strVmVersion.indexOf('.')));
if (vmVersion >= 17) {
long maxMemory = 0;
for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
maxMemory += mp.getUsage().getMax();
}
if (maxMemory < 30L * 1024 * 1024 * 1024) {
// for all memory pools (yes, including code cache).
return new MemoryLayoutSpecification() {
@Override
public int getArrayHeaderSize() {
return 16;
}
@Override
public int getObjectHeaderSize() {
return 12;
}
@Override
public int getObjectPadding() {
return 8;
}
@Override
public int getReferenceSize() {
return 4;
}
@Override
public int getSuperclassFieldPadding() {
return 4;
}
};
}
}
// In other cases, it's a 64-bit uncompressed OOPs object model
return new MemoryLayoutSpecification() {
@Override
public int getArrayHeaderSize() {
return 24;
}
@Override
public int getObjectHeaderSize() {
return 16;
}
@Override
public int getObjectPadding() {
return 8;
}
@Override
public int getReferenceSize() {
return 8;
}
@Override
public int getSuperclassFieldPadding() {
return 8;
}
};
}
use of java.lang.management.MemoryPoolMXBean in project zm-mailbox by Zimbra.
the class MemoryStats method dumpMemoryPools.
/**
* Output human-readable stats about the memory pools in the system
*
* @return
*/
public static String dumpMemoryPools() {
StringBuilder sb = new StringBuilder();
long totalUsed = 0;
long totalReserved = 0;
long totalMax = 0;
long collectUsed = 0;
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean pool : pools) {
MemoryUsage usage = pool.getUsage();
if (pool.getType() != MemoryType.HEAP) {
continue;
}
sb.append(new Formatter().format("\t\"%s\" memory used: %,d reserved: %,d max: %,d", pool.getName(), usage.getUsed(), usage.getCommitted(), usage.getMax()));
totalUsed += usage.getUsed();
totalReserved += usage.getCommitted();
totalMax += usage.getMax();
MemoryUsage collect = pool.getCollectionUsage();
if (collect != null) {
sb.append(new Formatter().format(" collectUsed: %,d", collect.getUsed()));
if (collect.getUsed() > 0) {
collectUsed += collect.getUsed();
} else {
collectUsed += usage.getUsed();
}
} else {
collectUsed += usage.getUsed();
}
sb.append('\n');
}
sb.append(new Formatter().format("RuntimeTotal=%,d RuntimeMax=%,d RuntimeFree=%,d TotUsed=%,d TotReserved=%,d TotMax=%,d CollectUsed=%,d\n", Runtime.getRuntime().totalMemory(), Runtime.getRuntime().maxMemory(), Runtime.getRuntime().freeMemory(), totalUsed, totalReserved, totalMax, collectUsed));
return sb.toString();
}
use of java.lang.management.MemoryPoolMXBean in project zm-mailbox by Zimbra.
the class MemoryStats method getHeapPoolNames.
/**
* @return A list of the HEAP memory pools in the VM, that is the pools used for dynamic
* allocations. Non-HEAP pools (code cache, permanent gen, etc) are not included
*
* For HotSpot default settings, the memory pools are:
* Eden (young gen) -- new allocs go here. Managed by Copy Collector.
* Survivor
* Tenured -- long-lived objects. Managed by MarkSweepCompact Collector
*/
public static String[] getHeapPoolNames() {
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
ArrayList<String> array = new ArrayList<String>(pools.size());
for (MemoryPoolMXBean pool : pools) {
// for bug 16398, include non-heap pools (so we get permgen info)
// if (pool.getType() != MemoryType.HEAP) {
// continue;
// }
array.add(pool.getName());
}
String[] toRet = new String[array.size()];
return array.toArray(toRet);
}
use of java.lang.management.MemoryPoolMXBean in project zm-mailbox by Zimbra.
the class MemoryStats method getStatData.
/* (non-Javadoc)
* @see com.zimbra.common.stats.RealtimeStatsCallback#getStatData()
*/
public Map<String, Object> getStatData() {
Map<String, Object> toRet = new HashMap<String, Object>();
// GC times
long minorCount = 0, minorTime = 0, majorCount = 0, majorTime = 0;
List<GarbageCollectorMXBean> gcs = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gc : gcs) {
String gcName = gc.getName();
long count = gc.getCollectionCount();
long time = gc.getCollectionTime();
String gcNameLower = gcName.toLowerCase();
toRet.put(getGCCountColName(gcNameLower), count);
toRet.put(getGCTimeColName(gcNameLower), time);
if (sMajorCollectors.contains(gcName)) {
majorCount += count;
majorTime += time;
} else {
minorCount += count;
minorTime += time;
}
}
toRet.put(GC_MINOR_COUNT, minorCount);
toRet.put(GC_MINOR_TIME, minorTime);
toRet.put(GC_MAJOR_COUNT, majorCount);
toRet.put(GC_MAJOR_TIME, majorTime);
long heapTotal = 0;
long heapUsed = 0;
// mempool stats
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean pool : pools) {
// for bug 16398, include non-heap pools (so we get permgen info)
// if (pool.getType() != MemoryType.HEAP) {
// continue;
// }
String poolName = pool.getName().toLowerCase();
long committed;
long used;
try {
MemoryUsage usage = pool.getUsage();
committed = usage.getCommitted();
used = usage.getUsed();
} catch (IllegalArgumentException e) {
// bug 31685, on java 1.5 we're getting an exception in the MemoryPoolMXBean:
// java.lang.IllegalArgumentException: committed = 494993408 should be < max = 494927872
// try to parse out the committed and max values if they match the string that we know.
String msg = e.getMessage();
Pattern p = Pattern.compile("committed = (\\d+) should be < max = (\\d+)");
Matcher m = p.matcher(msg);
if (m.find()) {
committed = Long.parseLong(m.group(1));
used = Long.parseLong(m.group(2));
} else {
committed = 0;
used = 0;
}
}
if (pool.getType() == MemoryType.HEAP) {
heapTotal += committed;
heapUsed += used;
}
long curpoolFree = committed - used;
toRet.put(getPoolUsedSizeColName(poolName), used);
toRet.put(getPoolFreeSizeColName(poolName), curpoolFree);
}
// heap stats
toRet.put(HEAP_USED, heapUsed);
toRet.put(HEAP_FREE, heapTotal - heapUsed);
return toRet;
}
Aggregations