use of java.lang.management.MemoryMXBean in project orientdb by orientechnologies.
the class SpeedTestData method collectResults.
/*
* (non-Javadoc)
*
* @see com.orientechnologies.common.test.SpeedTest#collectResults(long)
*/
public void collectResults(final long elapsed) {
Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
final MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
final MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
final int objectsPendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount();
final long nowHeapCommittedMemory = heapMemoryUsage.getCommitted();
final long nowHeapUsedMemory = heapMemoryUsage.getUsed();
final long nowHeapMaxMemory = heapMemoryUsage.getMax();
final long heapCommittedMemory = nowHeapCommittedMemory - currentTestHeapCommittedMemory;
final long heapUsedMemory = nowHeapUsedMemory - currentTestHeapUsedMemory;
final long heapMaxMemory = nowHeapMaxMemory - currentTestHeapMaxMemory;
final long nowNonHeapCommittedMemory = nonHeapMemoryUsage.getCommitted();
final long nowNonHeapUsedMemory = nonHeapMemoryUsage.getUsed();
final long nowNonHeapMaxMemory = nonHeapMemoryUsage.getMax();
final long nonHeapCommittedMemory = nowNonHeapCommittedMemory - currentTestNonHeapCommittedMemory;
final long nonHeapUsedMemory = nowNonHeapUsedMemory - currentTestNonHeapUsedMemory;
final long nonHeapMaxMemory = nowNonHeapMaxMemory - currentTestNonHeapMaxMemory;
if (printResults) {
System.out.println();
System.out.println(" Completed the test of '" + currentTestName + "' in " + elapsed + " ms. Heap memory used: " + nowHeapUsedMemory + " bytes. Non heap memory used: " + nowNonHeapUsedMemory + " .");
System.out.println(" Cycles done.......................: " + cyclesDone + "/" + cycles);
System.out.println(" Cycles Elapsed....................: " + cyclesElapsed + " ms");
System.out.println(" Elapsed...........................: " + elapsed + " ms");
System.out.println(" Medium cycle elapsed:.............: " + (cyclesDone > 0 && elapsed > 0 ? new BigDecimal((float) elapsed / cyclesDone).toPlainString() : 0));
System.out.println(" Cycles per second.................: " + new BigDecimal((float) cyclesDone / elapsed * 1000).toPlainString());
System.out.println(" Committed heap memory diff........: " + heapCommittedMemory + " (" + currentTestHeapCommittedMemory + "->" + nowHeapCommittedMemory + ")");
System.out.println(" Used heap memory diff.............: " + heapUsedMemory + " (" + currentTestHeapUsedMemory + "->" + nowHeapUsedMemory + ")");
System.out.println(" Max heap memory diff..............: " + heapMaxMemory + " (" + currentTestHeapMaxMemory + "->" + nowHeapMaxMemory + ")");
System.out.println(" Committed non heap memory diff....: " + nonHeapCommittedMemory + " (" + currentTestNonHeapCommittedMemory + "->" + nowNonHeapCommittedMemory + ")");
System.out.println(" Used non heap memory diff.........: " + nonHeapUsedMemory + " (" + currentTestNonHeapUsedMemory + "->" + nowNonHeapUsedMemory + ")");
System.out.println(" Max non heap memory diff..........: " + nonHeapMaxMemory + " (" + currentTestNonHeapMaxMemory + "->" + nowNonHeapMaxMemory + ")");
System.out.println(" Objects pending finalization......: " + objectsPendingFinalizationCount);
System.out.println();
}
if (testGroup != null) {
testGroup.setResult("Execution time", currentTestName, elapsed);
testGroup.setResult("Free memory", currentTestName, heapCommittedMemory);
}
currentTestHeapCommittedMemory = heapCommittedMemory;
currentTestHeapUsedMemory = heapUsedMemory;
currentTestHeapMaxMemory = heapMaxMemory;
currentTestNonHeapCommittedMemory = nonHeapCommittedMemory;
currentTestNonHeapUsedMemory = nonHeapUsedMemory;
currentTestNonHeapMaxMemory = nonHeapMaxMemory;
}
use of java.lang.management.MemoryMXBean in project jstorm by alibaba.
the class JStormUtils method getMemUsage.
public static Double getMemUsage() {
if (OSInfo.isLinux()) {
try {
Double value;
String pid = JStormUtils.process_pid();
String command = String.format("top -b -n 1 -p %s | grep -w %s", pid, pid);
String output = SystemOperation.exec(command);
int m = 0;
String[] strArray = output.split(" ");
for (int i = 0; i < strArray.length; i++) {
String info = strArray[i];
if (info.trim().length() == 0) {
continue;
}
if (m == 5) {
// memory
String unit = info.substring(info.length() - 1);
if (unit.equalsIgnoreCase("g")) {
value = Double.parseDouble(info.substring(0, info.length() - 1));
value *= 1000000000;
} else if (unit.equalsIgnoreCase("m")) {
value = Double.parseDouble(info.substring(0, info.length() - 1));
value *= 1000000;
} else {
value = Double.parseDouble(info);
}
//LOG.info("!!!! Get Memory Size:{}, info:{}", value, info);
return value;
}
if (m == 8) {
// cpu usage
}
if (m == 9) {
// memory ratio
}
m++;
}
} catch (Exception e) {
LOG.warn("Failed to get memory usage .");
}
}
// this will be incorrect
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
return (double) memoryUsage.getUsed();
}
use of java.lang.management.MemoryMXBean in project jstorm by alibaba.
the class JStormUtils method getJVMHeapMemory.
public static double getJVMHeapMemory() {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
return (double) memoryUsage.getUsed();
}
use of java.lang.management.MemoryMXBean in project commons by twitter.
the class JvmStats method register.
/**
* Add a series of system and jvm-level stats to the given registry.
*/
public static void register(MetricRegistry registry) {
final MetricRegistry stats = registry.scope("jvm");
final MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
// memory stats
final MetricRegistry heapRegistry = stats.scope("heap");
registerMemoryStats(heapRegistry, new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return mem.getHeapMemoryUsage();
}
});
final MetricRegistry nonHeapRegistry = stats.scope("nonheap");
registerMemoryStats(nonHeapRegistry, new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return mem.getNonHeapMemoryUsage();
}
});
// threads
final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
final MetricRegistry threadRegistry = stats.scope("thread");
threadRegistry.register(new AbstractGauge<Integer>("daemon_count") {
@Override
public Integer read() {
return threads.getDaemonThreadCount();
}
});
threadRegistry.register(new AbstractGauge<Integer>("count") {
@Override
public Integer read() {
return threads.getThreadCount();
}
});
threadRegistry.register(new AbstractGauge<Integer>("peak_count") {
@Override
public Integer read() {
return threads.getPeakThreadCount();
}
});
// class loading bean
final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
stats.register(new AbstractGauge<Integer>("classes_loaded") {
@Override
public Integer read() {
return classLoadingBean.getLoadedClassCount();
}
});
stats.register(new AbstractGauge<Long>("total_classes_loaded") {
@Override
public Long read() {
return classLoadingBean.getTotalLoadedClassCount();
}
});
stats.register(new AbstractGauge<Long>("classes_unloaded") {
@Override
public Long read() {
return classLoadingBean.getUnloadedClassCount();
}
});
// runtime
final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
stats.register(new AbstractGauge<Long>("start_time") {
@Override
public Long read() {
return runtime.getStartTime();
}
});
stats.register(new AbstractGauge<Long>("uptime") {
@Override
public Long read() {
return runtime.getUptime();
}
});
//stats.register(new AbstractGauge<String>)
// os
final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
stats.register(new AbstractGauge<Integer>("num_cpus") {
@Override
public Integer read() {
return os.getAvailableProcessors();
}
});
if (os instanceof com.sun.management.OperatingSystemMXBean) {
final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) os;
// if this is indeed an operating system
stats.register(new AbstractGauge<Long>("free_physical_memory") {
@Override
public Long read() {
return sunOsMbean.getFreePhysicalMemorySize();
}
});
stats.register(new AbstractGauge<Long>("free_swap") {
@Override
public Long read() {
return sunOsMbean.getFreeSwapSpaceSize();
}
});
stats.register(new AbstractGauge<Long>("process_cpu_time") {
@Override
public Long read() {
return sunOsMbean.getProcessCpuTime();
}
});
}
if (os instanceof com.sun.management.UnixOperatingSystemMXBean) {
// it's a unix system... I know this!
final UnixOperatingSystemMXBean unix = (UnixOperatingSystemMXBean) os;
stats.register(new AbstractGauge<Long>("fd_count") {
@Override
public Long read() {
return unix.getOpenFileDescriptorCount();
}
});
stats.register(new AbstractGauge<Long>("fd_limit") {
@Override
public Long read() {
return unix.getMaxFileDescriptorCount();
}
});
}
// mem
final List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
final MetricRegistry memRegistry = stats.scope("mem");
final MetricRegistry currentMem = memRegistry.scope("current");
final MetricRegistry postGCRegistry = memRegistry.scope("postGC");
for (final MemoryPoolMXBean pool : memPool) {
String name = normalizeName(pool.getName());
registerMemoryStats(currentMem.scope(name), new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return pool.getUsage();
}
});
registerMemoryStats(postGCRegistry.scope(name), new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return pool.getCollectionUsage();
}
});
}
currentMem.register(new AbstractGauge<Long>("used") {
@Override
public Long read() {
long sum = 0;
for (MemoryPoolMXBean pool : memPool) {
MemoryUsage usage = pool.getUsage();
if (usage != null) {
sum += usage.getUsed();
}
}
return sum;
}
});
AbstractGauge<Long> totalPostGCGauge = new AbstractGauge<Long>("used") {
@Override
public Long read() {
long sum = 0;
for (MemoryPoolMXBean pool : memPool) {
MemoryUsage usage = pool.getCollectionUsage();
if (usage != null) {
sum += usage.getUsed();
}
}
return sum;
}
};
postGCRegistry.register(totalPostGCGauge);
// java 1.7 specific buffer pool gauges
Multimap<String, AbstractGauge<Long>> java17gauges = buildJava17Gauges();
if (!java17gauges.isEmpty()) {
MetricRegistry bufferRegistry = stats.scope("buffer");
for (String scope : java17gauges.keySet()) {
MetricRegistry pool = bufferRegistry.scope(scope);
for (AbstractGauge<Long> gauge : java17gauges.get(scope)) {
pool.register(gauge);
}
}
}
// gc
final List<GarbageCollectorMXBean> gcPool = ManagementFactory.getGarbageCollectorMXBeans();
MetricRegistry gcRegistry = stats.scope("gc");
for (final GarbageCollectorMXBean gc : gcPool) {
String name = normalizeName(gc.getName());
MetricRegistry scoped = memRegistry.scope(name);
scoped.register(new AbstractGauge<Long>("cycles") {
@Override
public Long read() {
return gc.getCollectionCount();
}
});
scoped.register(new AbstractGauge<Long>("msec") {
@Override
public Long read() {
return gc.getCollectionTime();
}
});
}
gcRegistry.register(new AbstractGauge<Long>("cycles") {
@Override
public Long read() {
long sum = 0;
for (GarbageCollectorMXBean pool : gcPool) {
long count = pool.getCollectionCount();
if (count > 0) {
sum += count;
}
}
return sum;
}
});
gcRegistry.register(new AbstractGauge<Long>("msec") {
@Override
public Long read() {
long sum = 0;
for (GarbageCollectorMXBean pool : gcPool) {
long msec = pool.getCollectionTime();
if (msec > 0) {
sum += msec;
}
}
return sum;
}
});
}
use of java.lang.management.MemoryMXBean in project trie4j by takawitter.
the class AllTries method main.
public static void main(String[] args) throws Throwable {
MemoryMXBean mb = ManagementFactory.getMemoryMXBean();
int numWarmups = 1;
int numExecutions = 3;
int entries = 0;
int chars = 0;
for (String s : newWords()) {
entries++;
chars += s.length();
}
System.out.println("with " + entries + " words and " + chars + " chars in wikipedia titles.");
System.out.println("warming up... running all tries " + numWarmups + " times.");
for (AbstractProcess p : procs) {
System.out.println(p.getName());
}
for (int i = 0; i < numWarmups; i++) {
System.out.print((i + 1) + " ");
for (AbstractProcess p : procs) {
p.run();
System.gc();
}
}
System.out.println("... done.");
System.out.println("executiong each process " + numExecutions + " times.");
for (AbstractProcess p : procs) {
System.out.print(p.getName());
mb.gc();
mb.gc();
long b = 0, c = 0;
Object trie = null;
for (int i = 0; i < numExecutions; i++) {
Trio<Object, Long, Long> r = p.run();
b += r.getSecond();
c += r.getThird();
trie = r.getFirst();
mb.gc();
mb.gc();
}
System.out.println(String.format(", %d, %d, %d", b / numExecutions, c / numExecutions, mb.getHeapMemoryUsage().getUsed()));
// System.out.println("sleeping...");
// Thread.sleep(10000);
trie.hashCode();
trie = null;
}
//*
//*/
/*
{ // optimized double array
lt.lap();
OptimizedTailDoubleArray da = new OptimizedTailDoubleArray(pattrie, 65536, new SuffixTrieTailBuilder());
long b = lt.lap();
long c = lapContains(da);
log("OptimizedTailDoubleArray(suffixTrieTail), %d, %d", b, c);
}
System.gc();
System.gc();
{ // optimized double array with simple tb
lt.lap();
OptimizedTailDoubleArray da = new OptimizedTailDoubleArray(pattrie, 655536, new ConcatTailBuilder());
long b = lt.lap();
long c = lapContains(da);
log("OptimizedTailDoubleArray(concatTail), %d, %d", b, c);
}
System.gc();
System.gc();
//*/
}
Aggregations