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;
}
use of java.lang.management.MemoryMXBean in project geode by apache.
the class MXMemoryPoolListenerExample method main.
public static void main(String[] args) {
final MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
final double threshold;
{
double t = 0.8;
if (args.length > 0) {
try {
t = Integer.parseInt(args[0]) / 100;
} catch (NumberFormatException useDefault) {
}
}
if (t < 0.0 || t > 1.0) {
throw new IllegalArgumentException("Theshold must be >= 0 and <= 100");
}
threshold = t;
}
final int percentTenured;
{
int p = 100;
if (args.length > 1) {
try {
p = Integer.parseInt(args[1]);
} catch (NumberFormatException useDefault) {
}
}
if (p > 100 || p < 0) {
throw new IllegalArgumentException("Percent Tenured must be >= 0 and <= 100");
}
percentTenured = p;
}
Properties dsProps = new Properties();
// Loner
dsProps.setProperty(MCAST_PORT, "0");
dsProps.setProperty(ConfigurationProperties.LOG_LEVEL, "info");
dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLE_RATE, "200");
dsProps.setProperty(ConfigurationProperties.ENABLE_TIME_STATISTICS, "true");
dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLING_ENABLED, "true");
DistributedSystem ds = DistributedSystem.connect(dsProps);
final LogWriter logger = ds.getLogWriter();
logger.info("Usage threshold: " + threshold + "; percent tenured: " + percentTenured + "; Runtime Maximum memory: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + "Mb" + "; Heap Maximum memory: " + (mbean.getHeapMemoryUsage().getMax() / (1024 * 1024)) + "Mb");
MXMemoryPoolListenerExample me = new MXMemoryPoolListenerExample(ds);
// Register this listener to NotificationEmitter
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(me, null, null);
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p : pools) {
if (p.isCollectionUsageThresholdSupported()) {
// p.setCollectionUsageThreshold(0);
logger.info("Pool which supports collection usage threshold: " + p.getName() + "; " + p.getCollectionUsage());
}
// On JRockit do not set the usage threshold on the Nursery pool
if (p.getType().equals(MemoryType.HEAP) && p.isUsageThresholdSupported() && !p.getName().startsWith("Nursery")) {
int byteThreshold = (int) Math.ceil(threshold * p.getUsage().getMax());
logger.info("Setting threshold " + (byteThreshold / (1024 * 1024)) + "Mb on: " + p.getName() + "; " + p.getCollectionUsage());
p.setUsageThreshold(byteThreshold);
}
}
final Cache c = CacheFactory.create(ds);
new MemoryHog("hog_1", c, me.critical).consumeMemory(percentTenured).printTenuredSize();
ds.disconnect();
}
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.<CallbackMetric<?>>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());
});
}
use of java.lang.management.MemoryMXBean in project karaf by apache.
the class EnvironmentDumpProvider method dumpMemoryInformation.
private void dumpMemoryInformation(final PrintWriter outPW) {
final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
if (null == mxBean) {
return;
}
final MemoryUsage heapMemoryUsage = mxBean.getHeapMemoryUsage();
final MemoryUsage nonHeapMemoryUsage = mxBean.getNonHeapMemoryUsage();
if (heapMemoryUsage != null) {
outPW.println("HEAP Memory:");
outPW.printf(INDENT_KEY_VALUE_FORMAT, "commited", printMemory(heapMemoryUsage.getCommitted())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "init", printMemory(heapMemoryUsage.getInit())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "used", printMemory(heapMemoryUsage.getUsed())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "maximal", printMemory(heapMemoryUsage.getMax())).println();
}
if (nonHeapMemoryUsage != null) {
outPW.println("NON-HEAP Memory:");
outPW.printf(INDENT_KEY_VALUE_FORMAT, "commited", printMemory(nonHeapMemoryUsage.getCommitted())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "init", printMemory(nonHeapMemoryUsage.getInit())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "used", printMemory(nonHeapMemoryUsage.getUsed())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "maximal", printMemory(nonHeapMemoryUsage.getMax())).println();
}
}
use of java.lang.management.MemoryMXBean in project karaf by apache.
the class MemoryDumpProvider method writeDump.
@Override
protected void writeDump(OutputStreamWriter outputStream) throws Exception {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
outputStream.write("Number of objects waiting finalization: " + memoryMXBean.getObjectPendingFinalizationCount() + "\n\n");
outputStream.write("Heap:\n");
outputStream.write("\tInit: " + memoryMXBean.getHeapMemoryUsage().getInit() + "\n");
outputStream.write("\tUser: " + memoryMXBean.getHeapMemoryUsage().getUsed() + "\n");
outputStream.write("\tCommitted: " + memoryMXBean.getHeapMemoryUsage().getCommitted() + "\n");
outputStream.write("\tMax: " + memoryMXBean.getHeapMemoryUsage().getMax() + "\n");
outputStream.write("Non-Heap: \n");
outputStream.write("\tInit: " + memoryMXBean.getNonHeapMemoryUsage().getInit() + "\n");
outputStream.write("\tUser: " + memoryMXBean.getNonHeapMemoryUsage().getUsed() + "\n");
outputStream.write("\tCommitted: " + memoryMXBean.getNonHeapMemoryUsage().getCommitted() + "\n");
outputStream.write("\tMax: " + memoryMXBean.getNonHeapMemoryUsage().getMax() + "\n");
}
Aggregations