Java Code Examples for java.lang.management.MemoryUsage#getCommitted()

The following examples show how to use java.lang.management.MemoryUsage#getCommitted() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryData.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: MemoryLogger.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: MemoryPoolAdapter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
public long getSurvivorCommitted()
  throws JMException
{
  CompositeData data
    = (CompositeData) _mbeanServer.getAttribute(getSurvivorName(), "Usage");

  MemoryUsage usage = MemoryUsage.from(data);

  return usage.getCommitted();
}
 
Example 4
Source File: HealthModule.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 5
Source File: TestSurvivorRatioFlag.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: MonitoredDataImpl.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
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();
  }
}
 
Example 7
Source File: MemorySampler.java    From kieker with Apache License 2.0 5 votes vote down vote up
@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()), };
}
 
Example 8
Source File: TestShrinkDefragmentedHeap.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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
    );
}
 
Example 9
Source File: MemoryPoolImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 10
Source File: TestHumongousShrinkHeap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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
    );
}
 
Example 11
Source File: TestShrinkDefragmentedHeap.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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
    );
}
 
Example 12
Source File: MemoryPoolImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 13
Source File: MemoryPoolImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 14
Source File: MemoryUsageCompositeData.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 15
Source File: MemoryUsageCompositeData.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 16
Source File: VMInfo.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private long getMemoryPoolMaxOrCommited(MemoryPoolMXBean memoryPool) {
	MemoryUsage usage = memoryPool.getUsage();
	long max = usage.getMax();
	max = max < 0 ? usage.getCommitted() : max;
	return max;
}
 
Example 17
Source File: MemoryUsageCompositeData.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 18
Source File: MemoryUsageCompositeData.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 19
Source File: GemFireXDDataExtractorImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: ProactiveGcTask.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private long getMemoryPoolMaxOrCommitted(MemoryPoolMXBean memoryPool) {
	MemoryUsage usage = memoryPool.getUsage();
	long max = usage.getMax();
	return max < 0 ? usage.getCommitted() : max;
}