Search in sources :

Example 26 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project orientdb by orientechnologies.

the class SpeedTestData method startTimer.

/*
   * (non-Javadoc)
   * 
   * @see com.orientechnologies.common.test.SpeedTest#startTimer(java.lang.String)
   */
public void startTimer(final String iName) {
    Runtime.getRuntime().runFinalization();
    Runtime.getRuntime().gc();
    try {
        Thread.sleep(TIME_WAIT);
    } catch (InterruptedException e) {
    }
    final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    final MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    final MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
    currentTestName = iName;
    currentTestHeapCommittedMemory = heapMemoryUsage.getCommitted();
    currentTestHeapUsedMemory = heapMemoryUsage.getUsed();
    currentTestHeapMaxMemory = heapMemoryUsage.getMax();
    currentTestNonHeapCommittedMemory = nonHeapMemoryUsage.getCommitted();
    currentTestNonHeapUsedMemory = nonHeapMemoryUsage.getUsed();
    currentTestNonHeapMaxMemory = nonHeapMemoryUsage.getMax();
    System.out.println("-> Started the test of '" + currentTestName + "' (" + cycles + " cycles)");
    currentTestTimer = System.currentTimeMillis();
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 27 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project sonarqube by SonarSource.

the class ProcessStateSystemInfoTest method should_hide_attributes_without_values.

@Test
public void should_hide_attributes_without_values() {
    MemoryMXBean memoryBean = mock(MemoryMXBean.class, Mockito.RETURNS_DEEP_STUBS);
    when(memoryBean.getHeapMemoryUsage().getCommitted()).thenReturn(-1L);
    ProcessStateSystemInfo underTest = new ProcessStateSystemInfo(PROCESS_NAME);
    ProtobufSystemInfo.Section section = underTest.toProtobuf(memoryBean);
    assertThat(section.getAttributesList()).extracting("key").doesNotContain("Heap Committed (MB)");
}
Also used : ProtobufSystemInfo(org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo) MemoryMXBean(java.lang.management.MemoryMXBean) Test(org.junit.Test)

Example 28 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project springside4 by springside.

the class MetricExamples method gaugeExample.

@Test
public void gaugeExample() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    // 使用ConsoleReporter
    ConsoleReporter consoleReporter = new ConsoleReporter();
    final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    Gauge<Long> usedMemoryGague = new Gauge<Long>() {

        public Long getValue() {
            return memoryMXBean.getHeapMemoryUsage().getUsed();
        }
    };
    Gauge<Long> cachedUsedMemoryGague = new CachedGauge<Long>(10, TimeUnit.SECONDS) {

        public Long loadValue() {
            return memoryMXBean.getHeapMemoryUsage().getUsed();
        }
    };
    metricRegistry.registerGauge(MetricRegistry.name("JVM", "usedMemory"), usedMemoryGague);
    metricRegistry.registerGauge(MetricRegistry.name("JVM", "cachedUsedMemory"), cachedUsedMemoryGague);
    ReportScheduler scheduler = new ReportScheduler(metricRegistry, consoleReporter);
    scheduler.start(1, TimeUnit.SECONDS);
    Thread.sleep(1050);
    // use some memory
    List<Integer> list = new ArrayList<Integer>(200000);
    list.add(1);
    Thread.sleep(1050);
    scheduler.stop();
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) ConsoleReporter(org.springside.modules.metrics.reporter.ConsoleReporter) CachedGauge(org.springside.modules.metrics.metric.CachedGauge) ArrayList(java.util.ArrayList) Gauge(org.springside.modules.metrics.metric.Gauge) CachedGauge(org.springside.modules.metrics.metric.CachedGauge) Test(org.junit.Test)

Example 29 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project commons by twitter.

the class JvmStats method export.

/**
   * Exports stats related to the JVM and runtime environment.
   */
public static void export() {
    final OperatingSystemMXBean osMbean = ManagementFactory.getOperatingSystemMXBean();
    if (osMbean instanceof com.sun.management.OperatingSystemMXBean) {
        final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("system_free_physical_memory_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreePhysicalMemorySize() / BYTES_PER_MB;
            }
        }).add(new StatImpl<Long>("system_free_swap_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreeSwapSpaceSize() / BYTES_PER_MB;
            }
        }).add(Rate.of(new StatImpl<Long>("process_cpu_time_nanos") {

            @Override
            public Long read() {
                return sunOsMbean.getProcessCpuTime();
            }
        }).withName("process_cpu_cores_utilized").withScaleFactor(SECS_PER_NANO).build()).build());
    }
    if (osMbean instanceof com.sun.management.UnixOperatingSystemMXBean) {
        final com.sun.management.UnixOperatingSystemMXBean unixOsMbean = (com.sun.management.UnixOperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("process_max_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getMaxFileDescriptorCount();
            }
        }).add(new StatImpl<Long>("process_open_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getOpenFileDescriptorCount();
            }
        }).build());
    }
    final Runtime runtime = Runtime.getRuntime();
    final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("jvm_time_ms") {

        @Override
        public Long read() {
            return System.currentTimeMillis();
        }
    }).add(new StatImpl<Integer>("jvm_available_processors") {

        @Override
        public Integer read() {
            return runtime.availableProcessors();
        }
    }).add(new StatImpl<Long>("jvm_memory_free_mb") {

        @Override
        public Long read() {
            return runtime.freeMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_max_mb") {

        @Override
        public Long read() {
            return runtime.maxMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_mb_total") {

        @Override
        public Long read() {
            return runtime.totalMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Integer>("jvm_class_loaded_count") {

        @Override
        public Integer read() {
            return classLoadingBean.getLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_total_loaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getTotalLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_unloaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getUnloadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_time_ms") {

        @Override
        public Long read() {
            long collectionTimeMs = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collectionTimeMs += bean.getCollectionTime();
            }
            return collectionTimeMs;
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_count") {

        @Override
        public Long read() {
            long collections = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collections += bean.getCollectionCount();
            }
            return collections;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_uptime_secs") {

        @Override
        public Long read() {
            return runtimeMXBean.getUptime() / 1000;
        }
    }).add(new StatImpl<Double>("system_load_avg") {

        @Override
        public Double read() {
            return osMbean.getSystemLoadAverage();
        }
    }).add(new StatImpl<Integer>("jvm_threads_peak") {

        @Override
        public Integer read() {
            return threads.getPeakThreadCount();
        }
    }).add(new StatImpl<Long>("jvm_threads_started") {

        @Override
        public Long read() {
            return threads.getTotalStartedThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_daemon") {

        @Override
        public Integer read() {
            return threads.getDaemonThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_active") {

        @Override
        public Integer read() {
            return threads.getThreadCount();
        }
    }).build());
    // Export per memory pool gc time and cycle count like Ostrich
    // This is based on code in Bridcage: https://cgit.twitter.biz/birdcage/tree/ \
    // ostrich/src/main/scala/com/twitter/ostrich/stats/StatsCollection.scala
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_count") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionCount();
                }
            };
        }
    }));
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_time_ms") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionTime();
                }
            };
        }
    }));
    Stats.exportString(new StatImpl<String>("jvm_input_arguments") {

        @Override
        public String read() {
            return runtimeMXBean.getInputArguments().toString();
        }
    });
    for (final String property : System.getProperties().stringPropertyNames()) {
        Stats.exportString(new StatImpl<String>("jvm_prop_" + Stats.normalizeName(property)) {

            @Override
            public String read() {
                return System.getProperty(property);
            }
        });
    }
    for (final Map.Entry<String, String> environmentVariable : System.getenv().entrySet()) {
        Stats.exportString(new StatImpl<String>("system_env_" + Stats.normalizeName(environmentVariable.getKey())) {

            @Override
            public String read() {
                return environmentVariable.getValue();
            }
        });
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Function(com.google.common.base.Function) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 30 with MemoryMXBean

use of java.lang.management.MemoryMXBean in project opennms by OpenNMS.

the class JavaReportPlugin method getEntries.

@Override
public Map<String, Resource> getEntries() {
    final TreeMap<String, Resource> map = new TreeMap<String, Resource>();
    map.put("Class Version", getResourceFromProperty("java.class.version"));
    map.put("Compiler", getResourceFromProperty("java.compiler"));
    map.put("Home", getResourceFromProperty("java.home"));
    map.put("Version", getResourceFromProperty("java.version"));
    map.put("Vendor", getResourceFromProperty("java.vendor"));
    map.put("VM Version", getResourceFromProperty("java.vm.version"));
    map.put("VM Name", getResourceFromProperty("java.vm.name"));
    MemoryMXBean memoryBean = getBean(ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    if (memoryBean == null) {
        LOG.info("falling back to local VM MemoryMXBean");
        memoryBean = ManagementFactory.getMemoryMXBean();
    }
    addGetters(memoryBean, map);
    RuntimeMXBean runtimeBean = getBean(ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    if (runtimeBean == null) {
        LOG.info("falling back to local VM RuntimeMXBean");
        runtimeBean = ManagementFactory.getRuntimeMXBean();
    }
    addGetters(runtimeBean, map);
    ClassLoadingMXBean classBean = getBean(ManagementFactory.CLASS_LOADING_MXBEAN_NAME, ClassLoadingMXBean.class);
    if (classBean == null) {
        LOG.info("falling back to local VM ClassLoadingMXBean");
        classBean = ManagementFactory.getClassLoadingMXBean();
    }
    addGetters(classBean, map);
    return map;
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) Resource(org.springframework.core.io.Resource) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) TreeMap(java.util.TreeMap)

Aggregations

MemoryMXBean (java.lang.management.MemoryMXBean)38 MemoryUsage (java.lang.management.MemoryUsage)13 RuntimeMXBean (java.lang.management.RuntimeMXBean)10 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)7 ThreadMXBean (java.lang.management.ThreadMXBean)7 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3 NotificationEmitter (javax.management.NotificationEmitter)2 ObjectName (javax.management.ObjectName)2 MetricGroup (org.apache.flink.metrics.MetricGroup)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Function (com.google.common.base.Function)1 Description (com.google.gerrit.metrics.Description)1 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)1