Java Code Examples for com.sun.management.ThreadMXBean#getThreadAllocatedBytes()

The following examples show how to use com.sun.management.ThreadMXBean#getThreadAllocatedBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryLimits.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SurvivorAlignmentTestMain.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 3
Source File: SurvivorAlignmentTestMain.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 4
Source File: SurvivorAlignmentTestMain.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 5
Source File: AllThreadInfoQuestion.java    From tech-weekly with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
        ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
        long[] threadIds = threadMXBean.getAllThreadIds();

        for (long threadId : threadIds) {
//            ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
//            System.out.println(threadInfo.toString());
            long bytes = threadMXBean.getThreadAllocatedBytes(threadId);
            long kBytes = bytes / 1024;
            System.out.printf("线程[ID:%d] 分配内存: %s KB\n", threadId, kBytes);
        }

    }
 
Example 6
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 7
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 8
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks if threads other then the current thread were allocating objects
 * after baselinedThreadMemoryUsage call.
 *
 * If baselinedThreadMemoryUsage was not called, then this method will return
 * {@code false}.
 */
public boolean areOtherThreadsAllocatedMemory() {
    if (baselinedThreadMemoryUsage == null) {
        return false;
    }

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    long currentMemoryAllocation[]
            = bean.getThreadAllocatedBytes(threadIds);
    boolean otherThreadsAllocatedMemory = false;

    System.out.println("Verifying amount of memory allocated by threads:");
    for (int i = 0; i < threadIds.length; i++) {
        System.out.format("Thread %d%nbaseline allocation: %d"
                        + "%ncurrent allocation:%d%n", threadIds[i],
                baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
        System.out.println(bean.getThreadInfo(threadIds[i]));

        long bytesAllocated = Math.abs(currentMemoryAllocation[i]
                - baselinedThreadMemoryUsage[i]);
        if (bytesAllocated > 0
                && threadIds[i] != Thread.currentThread().getId()) {
            otherThreadsAllocatedMemory = true;
        }
    }

    return otherThreadsAllocatedMemory;
}
 
Example 9
Source File: TestThreadAllocationEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private long getThreadAllocatedBytes() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    return bean.getThreadAllocatedBytes(Thread.currentThread().getId());
}
 
Example 10
Source File: SurvivorAlignmentTestMain.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}
 
Example 11
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}
 
Example 12
Source File: CompilationStatistics.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static long getThreadAllocatedBytes() {
    ThreadMXBean thread = (ThreadMXBean) Management.getThreadMXBean();
    return thread.getThreadAllocatedBytes(currentThread().getId());
}
 
Example 13
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}
 
Example 14
Source File: TestThreadAllocationEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private long getThreadAllocatedBytes() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    return bean.getThreadAllocatedBytes(Thread.currentThread().getId());
}
 
Example 15
Source File: SurvivorAlignmentTestMain.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}
 
Example 16
Source File: SurvivorAlignmentTestMain.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}
 
Example 17
Source File: TestThreadAllocationEvent.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private long getThreadAllocatedBytes() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    return bean.getThreadAllocatedBytes(Thread.currentThread().getId());
}
 
Example 18
Source File: SurvivorAlignmentTestMain.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Baselines amount of memory allocated by each thread.
 */
public void baselineMemoryAllocation() {
    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    threadIds = bean.getAllThreadIds();
    baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
}