use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.
the class TestMemoryPoolMXBean method testSetCollectionUsageThresholdAttribute.
@Test
public void testSetCollectionUsageThresholdAttribute() {
MemoryPoolMXBean testImpl = testBean;
if (testImpl.isCollectionUsageThresholdSupported()) {
try {
long originalCUT = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
long newCUT = originalCUT + 1024;
Attribute newCUTAttr = new Attribute("CollectionUsageThreshold", new Long(newCUT));
mbs.setAttribute(objName, newCUTAttr);
AssertJUnit.assertEquals(new Long(newCUT), (Long) mbs.getAttribute(objName, "CollectionUsageThreshold"));
} catch (AttributeNotFoundException e) {
e.printStackTrace();
Assert.fail("Unexpected AttributeNotFoundException occurred: " + e.getMessage());
} catch (InstanceNotFoundException e) {
e.printStackTrace();
Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
} catch (MBeanException e) {
e.printStackTrace();
Assert.fail("Unexpected MBeanException occurred: " + e.getMessage());
} catch (ReflectionException e) {
e.printStackTrace();
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
} catch (InvalidAttributeValueException e) {
e.printStackTrace();
Assert.fail("Unexpected InvalidAttributeValueException occurred: " + e.getMessage());
}
} else {
try {
Attribute newCUTAttr = new Attribute("CollectionUsageThreshold", new Long(100 * 1024));
mbs.setAttribute(objName, newCUTAttr);
Assert.fail("Unreacheable code: should have thrown exception!");
} catch (Exception e) {
AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
logger.debug("Exception occurred, as expected: cannot set attribute (CollectionUsageThreshold).");
}
}
// end else collection usage threshold is not supported
}
use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.
the class TestMemoryPoolMXBean method testSetAttributes.
@Test
public final void testSetAttributes() {
// Only two attributes can be set for this platform bean type
// - UsageThreshold and CollectionUsageThreshold
MemoryPoolMXBean testImpl = testBean;
AttributeList attList = new AttributeList();
Attribute newUT = new Attribute("UsageThreshold", new Long(100 * 1024));
attList.add(newUT);
AttributeList setAttrs = null;
try {
setAttrs = mbs.setAttributes(objName, attList);
} catch (InstanceNotFoundException e) {
Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
} catch (ReflectionException e) {
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
AssertJUnit.assertNotNull(setAttrs);
AssertJUnit.assertTrue(setAttrs.size() <= 1);
if (setAttrs.size() == 1) {
AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getName().equals("UsageThreshold"));
AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
long recoveredValue = (Long) ((Attribute) (setAttrs.get(0))).getValue();
AssertJUnit.assertEquals(100 * 1024, recoveredValue);
}
attList = new AttributeList();
Attribute newCUT = new Attribute("CollectionUsageThreshold", new Long(250 * 1024));
attList.add(newCUT);
try {
setAttrs = mbs.setAttributes(objName, attList);
} catch (InstanceNotFoundException e) {
Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
} catch (ReflectionException e) {
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
AssertJUnit.assertNotNull(setAttrs);
AssertJUnit.assertTrue(setAttrs.size() <= 1);
if (setAttrs.size() == 1) {
AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getName().equals("CollectionUsageThreshold"));
AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
long recoveredValue = (Long) ((Attribute) (setAttrs.get(0))).getValue();
AssertJUnit.assertEquals(250 * 1024, recoveredValue);
}
// A failure scenario - a non-existent attribute...
AttributeList badList = new AttributeList();
Attribute garbage = new Attribute("Bantry", new Long(2888));
badList.add(garbage);
try {
setAttrs = mbs.setAttributes(objName, badList);
} catch (InstanceNotFoundException e) {
Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
} catch (ReflectionException e) {
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
AssertJUnit.assertNotNull(setAttrs);
AssertJUnit.assertTrue(setAttrs.size() == 0);
// Another failure scenario - a non-writable attribute...
badList = new AttributeList();
try {
setAttrs = mbs.setAttributes(objName, badList);
} catch (InstanceNotFoundException e) {
Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
} catch (ReflectionException e) {
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
badList.add(garbage);
try {
setAttrs = mbs.setAttributes(objName, badList);
} catch (InstanceNotFoundException e) {
// An unlikely exception - if this occurs, we can't proceed with the test.
Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
} catch (ReflectionException e) {
// An unlikely exception - if this occurs, we can't proceed with the test.
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
AssertJUnit.assertNotNull(setAttrs);
AssertJUnit.assertTrue(setAttrs.size() == 0);
// Yet another failure scenario - a wrongly-typed attribute...
badList = new AttributeList();
garbage = new Attribute("CollectionUsageThreshold", new Boolean(true));
badList.add(garbage);
try {
setAttrs = mbs.setAttributes(objName, badList);
} catch (InstanceNotFoundException e) {
Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
} catch (ReflectionException e) {
Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
}
AssertJUnit.assertNotNull(setAttrs);
AssertJUnit.assertTrue(setAttrs.size() == 0);
}
use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.
the class TestMisc method testWhoSupportsMemoryUsageThresholds.
@Test
public final void testWhoSupportsMemoryUsageThresholds() {
List<MemoryPoolMXBean> allPools = ManagementFactory.getMemoryPoolMXBeans();
Iterator<MemoryPoolMXBean> it = allPools.iterator();
while (it.hasNext()) {
MemoryPoolMXBean pool = it.next();
logger.debug("Pool " + pool.getName() + ", supports usage" + " threshold ? " + pool.isUsageThresholdSupported() + ", " + "supports collection usage threshold ? " + pool.isCollectionUsageThresholdSupported());
}
}
use of java.lang.management.MemoryPoolMXBean in project Terasology by MovingBlocks.
the class HeapAllocationMode method getMetrics.
@Override
public String getMetrics() {
// Memory stats without using Runtime.getRuntime() for client side
builder.setLength(0);
builder.append(getName());
builder.append("\n");
for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mpBean.getType() == MemoryType.HEAP) {
MemoryUsage usage = mpBean.getUsage();
builder.append(String.format("Memory Heap: %s - Memory Usage: %.2f MB, Max Memory: %.2f MB \n", mpBean.getName(), usage.getUsed() / MB_SIZE, usage.getMax() / MB_SIZE));
}
}
return builder.toString();
}
use of java.lang.management.MemoryPoolMXBean in project hive by apache.
the class LlapDaemon method getTotalHeapSize.
public static long getTotalHeapSize() {
// runtime.getMax() gives a very different number from the actual Xmx sizing.
// you can iterate through the
// http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryPoolMXBean.html
// from java.lang.management to figure this out, but the hard-coded params in the llap run.sh
// result in 89% usable heap (-XX:NewRatio=8) + a survivor region which is technically not
// in the usable space.
long total = 0;
for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
long sz = mp.getUsage().getMax();
if (mp.getName().contains("Survivor")) {
// there are 2 survivor spaces
sz *= 2;
}
if (mp.getType().equals(MemoryType.HEAP)) {
total += sz;
}
}
// round up to the next MB
total += (total % (1024 * 1024));
return total;
}
Aggregations