下面列出了java.lang.management.MemoryUsage#getCommitted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the memory footprint of the JVM in a string representation.
*
* @return A string describing how much heap memory and direct memory are allocated and used.
*/
public static String getMemoryUsageStatsAsString(MemoryMXBean memoryMXBean) {
MemoryUsage heap = memoryMXBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memoryMXBean.getNonHeapMemoryUsage();
long heapUsed = heap.getUsed() >> 20;
long heapCommitted = heap.getCommitted() >> 20;
long heapMax = heap.getMax() >> 20;
long nonHeapUsed = nonHeap.getUsed() >> 20;
long nonHeapCommitted = nonHeap.getCommitted() >> 20;
long nonHeapMax = nonHeap.getMax() >> 20;
return String.format("Memory usage stats: [HEAP: %d/%d/%d MB, " +
"NON HEAP: %d/%d/%d MB (used/committed/max)]",
heapUsed, heapCommitted, heapMax, nonHeapUsed, nonHeapCommitted, nonHeapMax);
}
private void checkMemoryUsage (MemoryUsage memUsage, String usage_type, String connect_type) {
// Get the elements of a MemoryUsage object and check they are valid
long init = memUsage.getInit();
long used = memUsage.getUsed();
long commit = memUsage.getCommitted();
long max = memUsage.getMax();
String formatter = " ";
if(usage_type.contains("Non")){
formatter = " ";
} else if (usage_type.contains("Current")) {
formatter = " ";
} else if (usage_type.contains("Peak")) {
formatter = " ";
} else if (usage_type.contains("Collection")) {
formatter = " ";
}
out.println(" " + usage_type + " Memory Usage:" + formatter + memUsage.toString());
checkMemoryUsageElements(connect_type, usage_type, init, max, commit, used);
}
public void setUsageThreshold(long newThreshold) {
if (!isUsageThresholdSupported()) {
throw new UnsupportedOperationException(
"Usage threshold is not supported");
}
Util.checkControlAccess();
MemoryUsage usage = getUsage0();
if (newThreshold < 0) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold);
}
if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold +
" must be <= maxSize." +
" Committed = " + usage.getCommitted() +
" Max = " + usage.getMax());
}
synchronized (this) {
if (!usageSensorRegistered) {
// pass the sensor to VM to begin monitoring
usageSensorRegistered = true;
setPoolUsageSensor(usageSensor);
}
setUsageThreshold0(usageThreshold, newThreshold);
this.usageThreshold = newThreshold;
}
}
public void setUsageThreshold(long newThreshold) {
if (!isUsageThresholdSupported()) {
throw new UnsupportedOperationException(
"Usage threshold is not supported");
}
Util.checkControlAccess();
MemoryUsage usage = getUsage0();
if (newThreshold < 0) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold);
}
if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold +
" must be <= maxSize." +
" Committed = " + usage.getCommitted() +
" Max = " + usage.getMax());
}
synchronized (this) {
if (!usageSensorRegistered) {
// pass the sensor to VM to begin monitoring
usageSensorRegistered = true;
setPoolUsageSensor(usageSensor);
}
setUsageThreshold0(usageThreshold, newThreshold);
this.usageThreshold = newThreshold;
}
}
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
humanReadableByteCount(memusage.getInit(), false),
humanReadableByteCount(memusage.getUsed(), false),
humanReadableByteCount(memusage.getCommitted(), false),
freeratio * 100
);
}
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
humanReadableByteCount(memusage.getInit(), false),
humanReadableByteCount(memusage.getUsed(), false),
humanReadableByteCount(memusage.getCommitted(), false),
freeratio * 100
);
}
public long getSurvivorCommitted()
throws JMException
{
CompositeData data
= (CompositeData) _mbeanServer.getAttribute(getSurvivorName(), "Usage");
MemoryUsage usage = MemoryUsage.from(data);
return usage.getCommitted();
}
public void setUsageThreshold(long newThreshold) {
if (!isUsageThresholdSupported()) {
throw new UnsupportedOperationException(
"Usage threshold is not supported");
}
Util.checkControlAccess();
MemoryUsage usage = getUsage0();
if (newThreshold < 0) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold);
}
if (usage.getMax() != -1 && newThreshold > usage.getMax()) {
throw new IllegalArgumentException(
"Invalid threshold: " + newThreshold +
" must be <= maxSize." +
" Committed = " + usage.getCommitted() +
" Max = " + usage.getMax());
}
synchronized (this) {
if (!usageSensorRegistered) {
// pass the sensor to VM to begin monitoring
usageSensorRegistered = true;
setPoolUsageSensor(usageSensor);
}
setUsageThreshold0(usageThreshold, newThreshold);
this.usageThreshold = newThreshold;
}
}
public static void printMemoryUsage(String label) {
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
label,
NF.format(memusage.getInit()),
NF.format(memusage.getUsed()),
NF.format(memusage.getCommitted()),
freeratio * 100
);
}
@Override
protected IMonitoringRecord[] createNewMonitoringRecords(final long timestamp, final String hostname, final String vmName,
final IMonitoringController monitoringCtr) {
if (!monitoringCtr.isProbeActivated(SignatureFactory.createJVMMemSignature())) {
return new IMonitoringRecord[] {};
}
final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
final MemoryUsage heapMemoryUsage = memoryBean.getHeapMemoryUsage();
final MemoryUsage nonHeapMemoryUsage = memoryBean.getNonHeapMemoryUsage();
return new IMonitoringRecord[] { new MemoryRecord(timestamp, hostname, vmName, heapMemoryUsage.getMax(), heapMemoryUsage.getUsed(),
heapMemoryUsage.getCommitted(), heapMemoryUsage.getInit(), nonHeapMemoryUsage.getMax(), nonHeapMemoryUsage.getUsed(),
nonHeapMemoryUsage.getCommitted(), nonHeapMemoryUsage.getInit(), memoryBean.getObjectPendingFinalizationCount()), };
}
MonitoredDataImpl(JmxSupport jmxSupport,JvmMXBeans jmxModel) {
this(jmxSupport);
RuntimeMXBean runtimeBean = jmxModel.getRuntimeMXBean();
upTime = runtimeBean.getUptime();
ClassLoadingMXBean classBean = jmxModel.getClassLoadingMXBean();
ThreadMXBean threadBean = jmxModel.getThreadMXBean();
MemoryUsage mem = jmxModel.getMemoryMXBean().getHeapMemoryUsage();
MemoryPoolMXBean permBean = jmxSupport.getPermGenPool();
unloadedClasses = classBean.getUnloadedClassCount();
loadedClasses = classBean.getLoadedClassCount() + unloadedClasses;
sharedLoadedClasses = 0;
sharedUnloadedClasses = 0;
threadsDaemon = threadBean.getDaemonThreadCount();
threadsLive = threadBean.getThreadCount();
threadsLivePeak = threadBean.getPeakThreadCount();
threadsStarted = threadBean.getTotalStartedThreadCount();
applicationTime = 0;
genCapacity = new long[2];
genUsed = new long[2];
genMaxCapacity = new long[2];
genCapacity[0] = mem.getCommitted();
genUsed[0] = mem.getUsed();
genMaxCapacity[0] = mem.getMax();
if (permBean != null) {
MemoryUsage perm = permBean.getUsage();
genCapacity[1] = perm.getCommitted();
genUsed[1] = perm.getUsed();
genMaxCapacity[1] = perm.getMax();
}
}
private static void verifyG1SurvivorRatio(int expectedRatio) {
MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();
int regionSize = wb.g1RegionSize();
int youngListLength = (int) Math.max(NEW_SIZE / regionSize, 1);
int expectedSurvivorRegions = (int) Math.ceil(youngListLength / (double) expectedRatio);
int observedSurvivorRegions = (int) (survivorUsage.getCommitted() / regionSize);
if (expectedSurvivorRegions < observedSurvivorRegions) {
throw new RuntimeException("Expected amount of G1 survivor regions is "
+ expectedSurvivorRegions + ", but observed "
+ observedSurvivorRegions);
}
}
private static TextComponent generateMemoryPoolDiagram(MemoryUsage usage, MemoryUsage collectionUsage, int length) {
double used = usage.getUsed();
double collectionUsed = used;
if (collectionUsage != null) {
collectionUsed = collectionUsage.getUsed();
}
double committed = usage.getCommitted();
double max = usage.getMax();
int usedChars = (int) ((used * length) / max);
int collectionUsedChars = (int) ((collectionUsed * length) / max);
int committedChars = (int) ((committed * length) / max);
TextComponent.Builder line = TextComponent.builder(Strings.repeat("/", collectionUsedChars)).color(TextColor.GRAY);
if (usedChars > collectionUsedChars) {
line.append(TextComponent.of("|", TextColor.RED));
line.append(TextComponent.of(Strings.repeat("/", (usedChars - collectionUsedChars) - 1), TextColor.GRAY));
}
if (committedChars > usedChars) {
line.append(TextComponent.of(Strings.repeat(" ", (committedChars - usedChars) - 1)));
line.append(TextComponent.of("|", TextColor.YELLOW));
}
if (length > committedChars) {
line.append(TextComponent.of(Strings.repeat(" ", (length - committedChars))));
}
return TextComponent.builder("")
.append(TextComponent.of("[", TextColor.DARK_GRAY))
.append(line.build())
.append(TextComponent.of("]", TextColor.DARK_GRAY))
.build();
}
private long getMemoryPoolMaxOrCommitted(MemoryPoolMXBean memoryPool) {
MemoryUsage usage = memoryPool.getUsage();
long max = usage.getMax();
return max < 0 ? usage.getCommitted() : max;
}
protected int getNumberOfThreads (List<ServerInfo> serverInfoList, List<DiskStoreImpl> diskStores) {
/****
* As we extract one diskstore at a time for a given server.
* Here the logic is we find the largest disk-store and use its size as a basis for worst case heap memory required
* for salvaging a server at a given time.
* This helps in determining how may servers can we salvage in parallel for given heap memory.
* This is a very conservative estimate.
*/
long maxDiskStoreSizeOnDisk = 0;
int maxNumberOfServersInParallel = 1;
final double mbDiv = Math.pow(1024, 2);
for (ServerInfo serverInfo : serverInfoList) {
long maxDiskStoreSizeForServer = ExtractorUtils.getMaxDiskStoreSizeForServer(serverInfo, diskStores);
if (maxDiskStoreSizeOnDisk < maxDiskStoreSizeForServer) {
maxDiskStoreSizeOnDisk = maxDiskStoreSizeForServer;
}
}
logInfo("Maximum disk-store size on disk " + maxDiskStoreSizeOnDisk/mbDiv + " MB");
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapMemUsage = memBean.getHeapMemoryUsage();
long usedMemory = heapMemUsage.getUsed();
long committedMemory = heapMemUsage.getCommitted();
//For safety using the committedMemory for calculation, as it is the memory that is guaranteed to be available for the VM.
long availableMemory = (committedMemory - usedMemory);
logInfo("Available memory : " + availableMemory/mbDiv + " MB");
double maxMemoryPerServer = (2.2)*maxDiskStoreSizeOnDisk;
//Setting the lower limit
if (maxMemoryPerServer < 1) {
maxMemoryPerServer = 1;
}
logInfo("Estimated memory needed per server : " + maxMemoryPerServer/mbDiv + " MB");
if (availableMemory < maxMemoryPerServer) {
logWarning("Not enough memory to extract the server, extractor could possibly run out of memory");
}
maxNumberOfServersInParallel = (int) (availableMemory/maxMemoryPerServer);
if (maxNumberOfServersInParallel < 1) {
maxNumberOfServersInParallel = 1;
}
logInfo("Recommended number of threads to extract server(s) in parallel : " + maxNumberOfServersInParallel);
return maxNumberOfServersInParallel;
}
public static void createGoodCompositeData() throws Exception {
final int K = 1024;
// these values are synchronized with the item names
final Object[] values = {
new Long(5 * K), // committed
new Long(1 * K), // init
new Long(10 * K), // max
new Long(2 * K), // used
"Dummy",
"Dummy",
};
CompositeType muct =
new CompositeType("MyMemoryUsageCompositeType",
"CompositeType for MemoryUsage",
memoryUsageItemNames,
memoryUsageItemNames,
memoryUsageItemTypes);
CompositeData cd =
new CompositeDataSupport(muct,
memoryUsageItemNames,
values);
MemoryUsage u = MemoryUsage.from(cd);
if (u.getInit() != ((Long) values[INIT]).longValue()) {
throw new RuntimeException("init = " + u.getInit() +
" expected = " + values[INIT]);
}
if (u.getUsed() != ((Long) values[USED]).longValue()) {
throw new RuntimeException("used = " + u.getUsed() +
" expected = " + values[USED]);
}
if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
throw new RuntimeException("committed = " + u.getCommitted() +
" expected = " + values[COMMITTED]);
}
if (u.getMax() != ((Long) values[MAX]).longValue()) {
throw new RuntimeException("max = " + u.getMax() +
" expected = " + values[MAX]);
}
System.out.println(u);
}
private long getMemoryPoolMaxOrCommited(MemoryPoolMXBean memoryPool) {
MemoryUsage usage = memoryPool.getUsage();
long max = usage.getMax();
max = max < 0 ? usage.getCommitted() : max;
return max;
}
public static void createGoodCompositeData() throws Exception {
final int K = 1024;
// these values are synchronized with the item names
final Object[] values = {
new Long(5 * K), // committed
new Long(1 * K), // init
new Long(10 * K), // max
new Long(2 * K), // used
"Dummy",
"Dummy",
};
CompositeType muct =
new CompositeType("MyMemoryUsageCompositeType",
"CompositeType for MemoryUsage",
memoryUsageItemNames,
memoryUsageItemNames,
memoryUsageItemTypes);
CompositeData cd =
new CompositeDataSupport(muct,
memoryUsageItemNames,
values);
MemoryUsage u = MemoryUsage.from(cd);
if (u.getInit() != ((Long) values[INIT]).longValue()) {
throw new RuntimeException("init = " + u.getInit() +
" expected = " + values[INIT]);
}
if (u.getUsed() != ((Long) values[USED]).longValue()) {
throw new RuntimeException("used = " + u.getUsed() +
" expected = " + values[USED]);
}
if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
throw new RuntimeException("committed = " + u.getCommitted() +
" expected = " + values[COMMITTED]);
}
if (u.getMax() != ((Long) values[MAX]).longValue()) {
throw new RuntimeException("max = " + u.getMax() +
" expected = " + values[MAX]);
}
System.out.println(u);
}
public static void createGoodCompositeData() throws Exception {
final int K = 1024;
// these values are synchronized with the item names
final Object[] values = {
new Long(5 * K), // committed
new Long(1 * K), // init
new Long(10 * K), // max
new Long(2 * K), // used
"Dummy",
"Dummy",
};
CompositeType muct =
new CompositeType("MyMemoryUsageCompositeType",
"CompositeType for MemoryUsage",
memoryUsageItemNames,
memoryUsageItemNames,
memoryUsageItemTypes);
CompositeData cd =
new CompositeDataSupport(muct,
memoryUsageItemNames,
values);
MemoryUsage u = MemoryUsage.from(cd);
if (u.getInit() != ((Long) values[INIT]).longValue()) {
throw new RuntimeException("init = " + u.getInit() +
" expected = " + values[INIT]);
}
if (u.getUsed() != ((Long) values[USED]).longValue()) {
throw new RuntimeException("used = " + u.getUsed() +
" expected = " + values[USED]);
}
if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
throw new RuntimeException("committed = " + u.getCommitted() +
" expected = " + values[COMMITTED]);
}
if (u.getMax() != ((Long) values[MAX]).longValue()) {
throw new RuntimeException("max = " + u.getMax() +
" expected = " + values[MAX]);
}
System.out.println(u);
}
public static void createGoodCompositeData() throws Exception {
final int K = 1024;
// these values are synchronized with the item names
final Object[] values = {
new Long(5 * K), // committed
new Long(1 * K), // init
new Long(10 * K), // max
new Long(2 * K), // used
"Dummy",
"Dummy",
};
CompositeType muct =
new CompositeType("MyMemoryUsageCompositeType",
"CompositeType for MemoryUsage",
memoryUsageItemNames,
memoryUsageItemNames,
memoryUsageItemTypes);
CompositeData cd =
new CompositeDataSupport(muct,
memoryUsageItemNames,
values);
MemoryUsage u = MemoryUsage.from(cd);
if (u.getInit() != ((Long) values[INIT]).longValue()) {
throw new RuntimeException("init = " + u.getInit() +
" expected = " + values[INIT]);
}
if (u.getUsed() != ((Long) values[USED]).longValue()) {
throw new RuntimeException("used = " + u.getUsed() +
" expected = " + values[USED]);
}
if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
throw new RuntimeException("committed = " + u.getCommitted() +
" expected = " + values[COMMITTED]);
}
if (u.getMax() != ((Long) values[MAX]).longValue()) {
throw new RuntimeException("max = " + u.getMax() +
" expected = " + values[MAX]);
}
System.out.println(u);
}