Example usage for java.lang.management MemoryUsage getInit

List of usage examples for java.lang.management MemoryUsage getInit

Introduction

In this page you can find the example usage for java.lang.management MemoryUsage getInit.

Prototype

public long getInit() 

Source Link

Document

Returns the amount of memory in bytes that the Java virtual machine initially requests from the operating system for memory management.

Usage

From source file:jef.testbase.JefTester.java

private static void printMem(MemoryUsage m) {
    System.out.println("??:" + StringUtils.formatSize(m.getMax()));
    System.out.println("?:" + StringUtils.formatSize(m.getCommitted()));
    System.out.println("?:" + StringUtils.formatSize(m.getInit()));
    System.out.println(":" + StringUtils.formatSize(m.getUsed()));
}

From source file:org.soitoolkit.commons.mule.util.MiscUtil.java

private static void printMemUsage() {
    int mb = 1024 * 1024;

    MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
    MemoryUsage hm = mxb.getHeapMemoryUsage();
    MemoryUsage nhm = mxb.getNonHeapMemoryUsage();
    //      int finalizable = mxb.getObjectPendingFinalizationCount();

    logger.trace("Heap Memory:  init/used/committed/max=" + hm.getInit() / mb + "/" + hm.getUsed() / mb + "/"
            + hm.getCommitted() / mb + "/" + hm.getMax() / mb);
    logger.trace("Non-Heap Mem: init/used/committed/max=" + nhm.getInit() / mb + "/" + nhm.getUsed() / mb + "/"
            + nhm.getCommitted() / mb + "/" + nhm.getMax() / mb);
    //                 logger.trace("finalizable: " + finalizable);

    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();

    logger.trace("Used/Free/Total/Max:"
            //Print used memory
            + (runtime.totalMemory() - runtime.freeMemory()) / mb + "/"

            //Print free memory
            + runtime.freeMemory() / mb + "/"

            //Print total available memory
            + runtime.totalMemory() / mb + "/"

            //Print Maximum available memory
            + runtime.maxMemory() / mb);
}

From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java

private String format(MemoryUsage memoryUsage) {
    long init = memoryUsage.getInit();
    long used = memoryUsage.getUsed();
    long committed = memoryUsage.getCommitted();
    long max = memoryUsage.getMax();

    return String.format("  init:      %s\n  used:      %s\n  committed: %s\n  max:       %s\n", init, used,
            committed, max);/*from w ww  .j av a 2  s. co m*/
}

From source file:net.centro.rtb.monitoringcenter.metrics.system.jvm.JvmMemoryUsageMetricSet.java

JvmMemoryUsageMetricSet(final MemoryUsageProvider memoryUsageProvider) {
    Preconditions.checkNotNull(memoryUsageProvider);

    final MemoryUsage memoryUsage = memoryUsageProvider.get();
    Preconditions.checkNotNull(memoryUsage);

    Map<String, Metric> metricsByNames = new HashMap<>();

    if (memoryUsage.getInit() >= 0) {
        this.initialSizeInBytesGauge = new Gauge<Long>() {
            @Override/*  w  w  w.  j  a  v a  2s  .  c om*/
            public Long getValue() {
                return memoryUsageProvider.get().getInit();
            }
        };
        metricsByNames.put("initialInBytes", initialSizeInBytesGauge);
    }

    this.usedMemoryInBytesGauge = new Gauge<Long>() {
        @Override
        public Long getValue() {
            return memoryUsageProvider.get().getUsed();
        }
    };
    metricsByNames.put("usedInBytes", usedMemoryInBytesGauge);

    if (memoryUsage.getMax() >= 0) {
        this.maxAvailableMemoryInBytesGauge = new Gauge<Long>() {
            @Override
            public Long getValue() {
                return memoryUsageProvider.get().getMax();
            }
        };
        metricsByNames.put("maxAvailableInBytes", maxAvailableMemoryInBytesGauge);
    }

    this.committedMemoryInBytesGauge = new Gauge<Long>() {
        @Override
        public Long getValue() {
            return memoryUsageProvider.get().getCommitted();
        }
    };
    metricsByNames.put("committedInBytes", committedMemoryInBytesGauge);

    this.usedMemoryPercentageGauge = new Gauge<Double>() {
        @Override
        public Double getValue() {
            MemoryUsage memoryUsage = memoryUsageProvider.get();
            long max = memoryUsage.getMax() > 0 ? memoryUsage.getMax() : memoryUsage.getCommitted();
            return Double.valueOf(memoryUsage.getUsed()) / max * 100;
        }
    };
    metricsByNames.put("usedPercentage", usedMemoryPercentageGauge);

    this.metricsByNames = metricsByNames;
}

From source file:com.snowstore.mercury.core.metric.SystemPublicMetrics.java

/**
 * Add JVM heap metrics./*from   ww w.j a va  2 s  .co m*/
 * 
 * @param result
 *            the result
 */
protected void addHeapMetrics(Collection<Metric<?>> result) {
    MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    result.add(new Metric<Long>("heap.committed", memoryUsage.getCommitted() / 1024));
    result.add(new Metric<Long>("heap.init", memoryUsage.getInit() / 1024));
    result.add(new Metric<Long>("heap.used", memoryUsage.getUsed() / 1024));
    result.add(new Metric<Long>("heap", memoryUsage.getMax() / 1024));
}

From source file:com.snowstore.mercury.core.metric.SystemPublicMetrics.java

/**
 * Add JVM heap metrics./*  w w w. j a  v  a 2  s  .c om*/
 * 
 * @param result
 *            the result
 */
protected void addNoHeapMetrics(Collection<Metric<?>> result) {
    MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
    result.add(new Metric<Long>("noheap.committed", memoryUsage.getCommitted() / 1024));
    result.add(new Metric<Long>("noheap.init", memoryUsage.getInit() / 1024));
    result.add(new Metric<Long>("noheap.used", memoryUsage.getUsed() / 1024));
    result.add(new Metric<Long>("noheap", memoryUsage.getMax() / 1024));
}

From source file:controllers.api.core.RootApiController.java

/**
 * Return some information regarding the memory status of the instance.
 *///from w ww  .  ja v a  2s.c  o m
@ApiAuthentication(onlyRootKey = true)
public Result instanceMemoryStatus() {

    try {

        RootResponse response = new RootResponse();

        response.attributes = new HashMap<String, JsonNode>();
        List<MemoryPoolMXBean> mbeans = ManagementFactory.getMemoryPoolMXBeans();
        if (mbeans != null) {
            for (MemoryPoolMXBean mbean : mbeans) {
                System.out.println(mbean.getName());
                MemoryUsage memUsage = mbean.getUsage();
                HashMap<String, Long> memoryUsageAsMap = new HashMap<String, Long>();
                memoryUsageAsMap.put("init", memUsage.getInit());
                memoryUsageAsMap.put("max", memUsage.getMax());
                memoryUsageAsMap.put("committed", memUsage.getCommitted());
                memoryUsageAsMap.put("used", memUsage.getUsed());
                response.attributes.put(mbean.getName(), Json.toJson(memoryUsageAsMap));
            }
        }

        return getJsonSuccessResponse(response);

    } catch (Exception e) {

        return getJsonErrorResponse(new ApiError(500, "INTERNAL SERVER ERROR", e));

    }
}

From source file:org.artifactory.webapp.wicket.page.config.advanced.SystemInfoPage.java

/**
 * Return a formatted string of the system info to display
 *
 * @return// w w  w.  ja v  a2  s .c o  m
 */
private String collectSystemInfo() {
    StringBuilder infoBuilder = new StringBuilder();

    StorageProperties storageProperties = ContextHelper.get().beanForType(StorageProperties.class);
    infoBuilder.append("Storage Info:").append("\n");
    addInfo(infoBuilder, "Database Type", storageProperties.getDbType().toString());
    BinaryProviderType binariesStorageType = storageProperties.getBinariesStorageType();
    addInfo(infoBuilder, "Storage Type", binariesStorageType.toString());
    if (BinaryProviderType.S3 == binariesStorageType) {
        // S3 properties
        addInfo(infoBuilder, "s3.bucket.name", storageProperties.getS3BucketName());
        addInfo(infoBuilder, "s3.bucket.path", storageProperties.getS3BucketPath());
        addInfo(infoBuilder, "s3.endpoint", storageProperties.getS3Entpoint());
        // Retry properties
        addInfo(infoBuilder, "retry.max.retries.number",
                Integer.toString(storageProperties.getMaxRetriesNumber()));
        addInfo(infoBuilder, "retry.delay.between.retries",
                Integer.toString(storageProperties.getDelayBetweenRetries()));
        // Eventually persisted properties
        addInfo(infoBuilder, "eventually.persisted.max.number.of.threads",
                Integer.toString(storageProperties.getEventuallyPersistedMaxNumberOfThread()));
        addInfo(infoBuilder, "eventually.persisted.timeout",
                Integer.toString(storageProperties.getEventuallyPersistedTimeOut()));
        addInfo(infoBuilder, "eventually.dispatcher.sleep.time",
                Long.toString(storageProperties.getEventuallyPersistedDispatcherSleepTime()));
    }

    infoBuilder.append("\n").append("System Properties:").append("\n");
    Properties properties = System.getProperties();
    //// add Artifactory version to the properties, will be alphabetically sorted later.
    properties.setProperty(ConstantValues.artifactoryVersion.getPropertyName(),
            ConstantValues.artifactoryVersion.getString());
    AddonsManager addonsManager = ContextHelper.get().beanForType(AddonsManager.class);
    addInfo(infoBuilder, "artifactory.running.mode", addonsManager.getArtifactoryRunningMode().name());
    addInfo(infoBuilder, "artifactory.running.state", ContextHelper.get().isOffline() ? "Offline" : "Online");
    addFromProperties(infoBuilder, properties);
    addHaProperties(infoBuilder);
    infoBuilder.append("\n").append("General JVM Info:").append("\n");
    OperatingSystemMXBean systemBean = ManagementFactory.getOperatingSystemMXBean();
    addInfo(infoBuilder, "Available Processors", Integer.toString(systemBean.getAvailableProcessors()));

    MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heapMemoryUsage = memoryBean.getHeapMemoryUsage();

    addInfo(infoBuilder, "Heap Memory Usage-Committed", Long.toString(heapMemoryUsage.getCommitted()));
    addInfo(infoBuilder, "Heap Memory Usage-Init", Long.toString(heapMemoryUsage.getInit()));
    addInfo(infoBuilder, "Heap Memory Usage-Max", Long.toString(heapMemoryUsage.getMax()));
    addInfo(infoBuilder, "Heap Memory Usage-Used", Long.toString(heapMemoryUsage.getUsed()));

    MemoryUsage nonHeapMemoryUsage = memoryBean.getNonHeapMemoryUsage();
    addInfo(infoBuilder, "Non-Heap Memory Usage-Committed", Long.toString(nonHeapMemoryUsage.getCommitted()));
    addInfo(infoBuilder, "Non-Heap Memory Usage-Init", Long.toString(nonHeapMemoryUsage.getInit()));
    addInfo(infoBuilder, "Non-Heap Memory Usage-Max", Long.toString(nonHeapMemoryUsage.getMax()));
    addInfo(infoBuilder, "Non-Heap Memory Usage-Used", Long.toString(nonHeapMemoryUsage.getUsed()));

    RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
    StringBuilder vmArgumentBuilder = new StringBuilder();
    List<String> vmArguments = RuntimemxBean.getInputArguments();
    if (vmArguments != null) {
        for (String vmArgument : vmArguments) {
            if (InfoWriter.shouldMaskValue(vmArgument)) {
                vmArgument = Strings.maskKeyValue(vmArgument);
            }
            vmArgumentBuilder.append(vmArgument);
            if (vmArguments.indexOf(vmArgument) != (vmArguments.size() - 1)) {
                vmArgumentBuilder.append("\n");
            }
        }
    }

    infoBuilder.append("\nJVM Arguments:\n").append(vmArgumentBuilder.toString());

    return StringUtils.removeEnd(infoBuilder.toString(), "\n");
}

From source file:org.craftercms.commons.monitoring.MemoryMonitor.java

/**
 * Private Constructor of the MemoryMonitor POJO
 * @param memName Type of MemoryMonitor to get the information.
 * @param memoryUsage {@link MemoryUsage} bean where the information is taken from.
 *///w w  w .j  av  a2 s. com
private MemoryMonitor(String memName, MemoryUsage memoryUsage) {
    this.name = memName;
    this.init = FileUtils.byteCountToDisplaySize(memoryUsage.getInit());
    this.used = FileUtils.byteCountToDisplaySize(memoryUsage.getUsed());
    this.committed = FileUtils.byteCountToDisplaySize(memoryUsage.getCommitted());
    this.max = FileUtils.byteCountToDisplaySize(memoryUsage.getMax());
}

From source file:org.eclipse.gyrex.cloud.internal.NodeMetricsReporter.java

@Override
protected IStatus run(final IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
    }//from  w w w .  ja v  a 2  s.c om

    try {
        final Properties metrics = new Properties() {
            private static final long serialVersionUID = 1L;

            @Override
            public synchronized Enumeration<Object> keys() {
                return Collections.enumeration(keySet());
            }

            @Override
            public Set<Object> keySet() {
                return new TreeSet<Object>(super.keySet());
            }
        };
        final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        metrics.setProperty("os.availableProcessors",
                String.valueOf(operatingSystemMXBean.getAvailableProcessors()));
        metrics.setProperty("os.systemLoadAverage",
                String.valueOf(operatingSystemMXBean.getSystemLoadAverage()));
        metrics.setProperty("os.committedVirtualMemorySize",
                getUsingReflection(operatingSystemMXBean, "getCommittedVirtualMemorySize"));
        metrics.setProperty("os.totalSwapSpaceSize",
                getUsingReflection(operatingSystemMXBean, "getTotalSwapSpaceSize"));
        metrics.setProperty("os.freeSwapSpaceSize",
                getUsingReflection(operatingSystemMXBean, "getFreeSwapSpaceSize"));
        metrics.setProperty("os.processCpuTime",
                getUsingReflection(operatingSystemMXBean, "getProcessCpuTime"));
        metrics.setProperty("os.freePhysicalMemorySize",
                getUsingReflection(operatingSystemMXBean, "getFreePhysicalMemorySize"));
        metrics.setProperty("os.totalPhysicalMemorySize",
                getUsingReflection(operatingSystemMXBean, "getTotalPhysicalMemorySize"));
        metrics.setProperty("os.openFileDescriptorCount",
                getUsingReflection(operatingSystemMXBean, "getOpenFileDescriptorCount"));
        metrics.setProperty("os.maxFileDescriptorCount",
                getUsingReflection(operatingSystemMXBean, "getMaxFileDescriptorCount"));

        final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        final MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        metrics.setProperty("heap.used", String.valueOf(heapMemoryUsage.getUsed()));
        metrics.setProperty("heap.committed", String.valueOf(heapMemoryUsage.getCommitted()));
        metrics.setProperty("heap.max", String.valueOf(heapMemoryUsage.getMax()));
        metrics.setProperty("heap.init", String.valueOf(heapMemoryUsage.getInit()));
        final MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        metrics.setProperty("nonHeap.used", String.valueOf(nonHeapMemoryUsage.getUsed()));
        metrics.setProperty("nonHeap.committed", String.valueOf(nonHeapMemoryUsage.getCommitted()));
        metrics.setProperty("nonHeap.max", String.valueOf(nonHeapMemoryUsage.getMax()));
        metrics.setProperty("nonHeap.init", String.valueOf(nonHeapMemoryUsage.getInit()));

        final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        metrics.setProperty("thread.count", String.valueOf(threadMXBean.getThreadCount()));
        metrics.setProperty("thread.peak", String.valueOf(threadMXBean.getPeakThreadCount()));
        metrics.setProperty("thread.totalStarted", String.valueOf(threadMXBean.getTotalStartedThreadCount()));

        final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        metrics.setProperty("uptime", String.valueOf(runtimeMXBean.getUptime()));

        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final NodeInfo nodeInfo = CloudState.getNodeInfo();
        metrics.store(out, String.valueOf(nodeInfo));
        ZooKeeperGate.get().writeRecord(IZooKeeperLayout.PATH_NODES_METRICS.append(nodeInfo.getNodeId()),
                CreateMode.PERSISTENT, out.toByteArray());
        if (CloudDebug.nodeMetrics) {
            LOG.debug("Node metrics reported successfully.{}{}", SystemUtils.LINE_SEPARATOR,
                    new String(out.toByteArray(), CharEncoding.ISO_8859_1));
        }
    } catch (final Exception e) {
        LOG.warn("Failed to update node metrics. {}", e.getMessage());
    } finally {
        // reschedule
        schedule(DELAY);
    }

    return Status.OK_STATUS;
}