use of java.lang.management.MemoryMXBean in project core-ng-project by neowu.
the class MemoryUsageController method memoryUsage.
private MemoryUsage memoryUsage() {
MemoryUsage usage = new MemoryUsage();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
java.lang.management.MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
usage.heapInit = heapMemoryUsage.getInit();
usage.heapUsed = heapMemoryUsage.getUsed();
usage.heapCommitted = heapMemoryUsage.getCommitted();
usage.heapMax = heapMemoryUsage.getMax();
java.lang.management.MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
usage.nonHeapInit = nonHeapMemoryUsage.getInit();
usage.nonHeapUsed = nonHeapMemoryUsage.getUsed();
usage.nonHeapCommitted = nonHeapMemoryUsage.getCommitted();
usage.nonHeapMax = nonHeapMemoryUsage.getMax();
return usage;
}
use of java.lang.management.MemoryMXBean in project java-chassis by ServiceComb.
the class HealthMonitorDataProvider method exactProcessInfo.
private void exactProcessInfo(MonitorData monitorData) {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memoryHeapUsage = memoryMXBean.getHeapMemoryUsage();
MemoryUsage memoryNonHeapUsage = memoryMXBean.getNonHeapMemoryUsage();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
int threadCount = threadMXBean.getThreadCount();
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double cpu = operatingSystemMXBean.getSystemLoadAverage();
monitorData.setCpu(CPUMonitorCalc.getInstance().getProcessCpu());
monitorData.setLoadAverage(cpu);
monitorData.setThreadCount(threadCount);
monitorData.setUptime(runtimeMXBean.getUptime());
Map<String, Long> memoryInfo = new HashMap<>();
memoryInfo.put("heapInit", memoryHeapUsage.getInit());
memoryInfo.put("headMax", memoryHeapUsage.getMax());
memoryInfo.put("heapCommit", memoryHeapUsage.getCommitted());
memoryInfo.put("heapUsed", memoryHeapUsage.getUsed());
memoryInfo.put("nonHeapInit", memoryNonHeapUsage.getInit());
memoryInfo.put("nonHeapCommit", memoryNonHeapUsage.getCommitted());
memoryInfo.put("nonHeapUsed", memoryNonHeapUsage.getUsed());
monitorData.setMemory(memoryInfo);
}
use of java.lang.management.MemoryMXBean in project jstorm by alibaba.
the class SystemBolt method prepare.
@Override
public void prepare(final Map stormConf, TopologyContext context, OutputCollector collector) {
if (_prepareWasCalled && !"local".equals(stormConf.get(Config.STORM_CLUSTER_MODE))) {
throw new RuntimeException("A single worker should have 1 SystemBolt instance.");
}
_prepareWasCalled = true;
int bucketSize = RT.intCast(stormConf.get(Config.TOPOLOGY_BUILTIN_METRICS_BUCKET_SIZE_SECS));
final RuntimeMXBean jvmRT = ManagementFactory.getRuntimeMXBean();
context.registerMetric("uptimeSecs", new IMetric() {
@Override
public Object getValueAndReset() {
return jvmRT.getUptime() / 1000.0;
}
}, bucketSize);
context.registerMetric("startTimeSecs", new IMetric() {
@Override
public Object getValueAndReset() {
return jvmRT.getStartTime() / 1000.0;
}
}, bucketSize);
context.registerMetric("newWorkerEvent", new IMetric() {
boolean doEvent = true;
@Override
public Object getValueAndReset() {
if (doEvent) {
doEvent = false;
return 1;
} else
return 0;
}
}, bucketSize);
final MemoryMXBean jvmMemRT = ManagementFactory.getMemoryMXBean();
context.registerMetric("memory/heap", new MemoryUsageMetric(new AFn() {
public Object invoke() {
return jvmMemRT.getHeapMemoryUsage();
}
}), bucketSize);
context.registerMetric("memory/nonHeap", new MemoryUsageMetric(new AFn() {
public Object invoke() {
return jvmMemRT.getNonHeapMemoryUsage();
}
}), bucketSize);
for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
context.registerMetric("GC/" + b.getName().replaceAll("\\W", ""), new GarbageCollectorMetric(b), bucketSize);
}
}
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();
}
});
}
}
use of java.lang.management.MemoryMXBean in project gerrit by GerritCodeReview.
the class ProcMetricModule method procJvmMemory.
private void procJvmMemory(MetricMaker metrics) {
CallbackMetric0<Long> heapCommitted = metrics.newCallbackMetric("proc/jvm/memory/heap_committed", Long.class, new Description("Amount of memory guaranteed for user objects.").setGauge().setUnit(Units.BYTES));
CallbackMetric0<Long> heapUsed = metrics.newCallbackMetric("proc/jvm/memory/heap_used", Long.class, new Description("Amount of memory holding user objects.").setGauge().setUnit(Units.BYTES));
CallbackMetric0<Long> nonHeapCommitted = metrics.newCallbackMetric("proc/jvm/memory/non_heap_committed", Long.class, new Description("Amount of memory guaranteed for classes, etc.").setGauge().setUnit(Units.BYTES));
CallbackMetric0<Long> nonHeapUsed = metrics.newCallbackMetric("proc/jvm/memory/non_heap_used", Long.class, new Description("Amount of memory holding classes, etc.").setGauge().setUnit(Units.BYTES));
CallbackMetric0<Integer> objectPendingFinalizationCount = metrics.newCallbackMetric("proc/jvm/memory/object_pending_finalization_count", Integer.class, new Description("Approximate number of objects needing finalization.").setGauge().setUnit("objects"));
MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
metrics.newTrigger(ImmutableSet.of(heapCommitted, heapUsed, nonHeapCommitted, nonHeapUsed, objectPendingFinalizationCount), () -> {
try {
MemoryUsage stats = memory.getHeapMemoryUsage();
heapCommitted.set(stats.getCommitted());
heapUsed.set(stats.getUsed());
} catch (IllegalArgumentException e) {
// MXBean may throw due to a bug in Java 7; ignore.
}
MemoryUsage stats = memory.getNonHeapMemoryUsage();
nonHeapCommitted.set(stats.getCommitted());
nonHeapUsed.set(stats.getUsed());
objectPendingFinalizationCount.set(memory.getObjectPendingFinalizationCount());
});
}
Aggregations