Search in sources :

Example 1 with ThreadMXBean

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;
}
Also used : ThreadMXBean(com.sun.management.ThreadMXBean)

Example 2 with ThreadMXBean

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);
}
Also used : ThreadMXBean(com.sun.management.ThreadMXBean) ArrayList(java.util.ArrayList) GridToStringBuilder.joinToString(org.apache.ignite.internal.util.tostring.GridToStringBuilder.joinToString) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 3 with ThreadMXBean

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);
}
Also used : ThreadMXBean(com.sun.management.ThreadMXBean)

Aggregations

ThreadMXBean (com.sun.management.ThreadMXBean)3 ArrayList (java.util.ArrayList)1 GridToStringBuilder.joinToString (org.apache.ignite.internal.util.tostring.GridToStringBuilder.joinToString)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)1 Test (org.junit.Test)1