use of com.sun.management.ThreadMXBean in project crate by crate.
the class MemoryLimits method assertMaxBytesAllocated.
/**
* Asserts that `supplier.get` doesn't allocate more than `bytes` bytes.
* This is limited to the current thread and cannot account for memory accounted in other threads.
*/
public static <T> T assertMaxBytesAllocated(long bytes, Supplier<T> supplier) {
ThreadMXBean threadMXBean = ManagementFactory.getPlatformMXBean(ThreadMXBean.class);
long threadId = Thread.currentThread().getId();
long allocatedBytesBegin = threadMXBean.getThreadAllocatedBytes(threadId);
T t = supplier.get();
long allocatedBytesAfter = threadMXBean.getThreadAllocatedBytes(threadId);
long allocatedBytes = allocatedBytesAfter - allocatedBytesBegin;
assertThat(allocatedBytes, Matchers.lessThanOrEqualTo(bytes));
return t;
}
use of com.sun.management.ThreadMXBean in project ignite by apache.
the class GridToStringBuilderSelfTest method testMemoryConsumption.
/**
* Test GridToStringBuilder memory consumption.
*/
@Test
public void testMemoryConsumption() {
int objCnt = 100;
ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
TestClass2 obj = new TestClass2(new String(new char[1_000_000]));
List<TestClass2> arr = new ArrayList<>(objCnt);
for (int i = 1; i <= objCnt; i++) arr.add(new TestClass2(new String(new char[i])));
GridToStringBuilder.toString(TestClass2.class, obj);
long allocated0 = bean.getThreadAllocatedBytes(Thread.currentThread().getId());
for (TestClass2 item : arr) GridToStringBuilder.toString(TestClass2.class, item);
long allocated1 = bean.getThreadAllocatedBytes(Thread.currentThread().getId());
log.info("Memory allocated by GridToStringBuilder for " + objCnt + " objects: " + (allocated1 - allocated0));
assertTrue("Too much memory allocated by GridToStringBuilder: " + (allocated1 - allocated0), allocated1 - allocated0 < 1_000_000);
}
use of com.sun.management.ThreadMXBean in project neo4j by neo4j.
the class SunManagementHeapAllocation method load.
/**
* Invoked from {@code HeapAllocation#load(java.lang.management.ThreadMXBean)} through reflection.
*/
@SuppressWarnings("unused")
static HeapAllocation load(java.lang.management.ThreadMXBean bean) {
checkArgument(bean instanceof ThreadMXBean, "The ThreadMXBean must be an instance of '" + ThreadMXBean.class.getName() + "'.");
ThreadMXBean threadMXBean = (ThreadMXBean) bean;
checkState(threadMXBean.isThreadAllocatedMemorySupported(), "Thread allocations not supported.");
return new SunManagementHeapAllocation(threadMXBean);
}
Aggregations