com.sun.management.ThreadMXBean Java Examples

The following examples show how to use com.sun.management.ThreadMXBean. 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: ServiceThread.java    From HolandaCatalinaFw with Apache License 2.0 6 votes vote down vote up
/**
 * Verify if the current thread allocate more bytes that the max configured.
 * @throws Throwable Throws a run time exception.
 */
public static void checkAllocatedMemory() throws Throwable {
    ServiceThread serviceThread = (ServiceThread) Thread.currentThread();
    if(serviceThread.getAccumulatedAllocatedMemory() > serviceThread.getMaxAllocatedMemory()) {

        //Reset the initial value for the current thread in order to
        //continue with the throwable handling
        serviceThread.setInitialAllocatedMemory(((ThreadMXBean)ManagementFactory.getThreadMXBean()).
                getThreadAllocatedBytes(Thread.currentThread().getId()));

        if(SystemProperties.getBoolean(SystemProperties.Service.MAX_ALLOCATED_MEMORY_EXCEEDED_THROWS_EXCEPTION)) {
            throw new RuntimeException("Max memory allocated for thread exceeded");
        } else {
            Log.w(SystemProperties.get("SERVICE_THREAD"), "Max memory allocated for thread exceeded");
        }
    }
}
 
Example #2
Source File: ServiceThread.java    From HolandaCatalinaFw with Apache License 2.0 6 votes vote down vote up
/**
 * Set the session for the thread.
 * @param session Service session.
 */
public final void setSession(ServiceSession session) {
    if(this.session != null) {
        //Remove the status of the current thread stored into the old session
        this.session.endThread();
    }

    if(session != null) {
        //Start the status of the current thread into the new session.
        session.startThread();

        //Init the counters
        setInitialAllocatedMemory(((ThreadMXBean)ManagementFactory.getThreadMXBean()).
                getThreadAllocatedBytes(Thread.currentThread().getId()));
        setInitialTime(ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime());

        //Init the max allocated memory value for thread
        configureMaxAllocatedMemory(SystemProperties.getLong(SystemProperties.Service.MAX_ALLOCATED_MEMORY_FOR_THREAD));

        //Init the max execution time value for thread
        configureMaxExecutionTime(SystemProperties.getLong(SystemProperties.Service.MAX_EXECUTION_TIME_FOR_THREAD));
    }

    this.session = session;
}
 
Example #3
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 #4
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 #5
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 #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-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 #8
Source File: TestThreadCpuTimeEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
    thread.start();

    List<RecordedEvent> beforeEvents = generateEvents(2, barrier);
    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}
 
Example #9
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 #10
Source File: JmxClient.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public synchronized ThreadMXBean getThreadMXBean() throws IOException {
	if (hasPlatformMXBeans && threadMBean == null) {
		threadMBean = JMX.newMXBeanProxy(server, createBeanName(ManagementFactory.THREAD_MXBEAN_NAME),
				ThreadMXBean.class);
	}
	return threadMBean;
}
 
Example #11
Source File: JmxClient.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public synchronized ThreadMXBean getThreadMXBean() throws IOException {
	if (hasPlatformMXBeans && threadMBean == null) {
		threadMBean = JMX.newMXBeanProxy(server, createBeanName(ManagementFactory.THREAD_MXBEAN_NAME),
				ThreadMXBean.class);
	}
	return threadMBean;
}
 
Example #12
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 #13
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 #14
Source File: TestThreadCpuTimeEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
    thread.start();

    List<RecordedEvent> beforeEvents = generateEvents(2, barrier);
    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}
 
Example #15
Source File: TestThreadCpuTimeEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Recording recording = new Recording();

    recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis));
    recording.start();

    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);

    // Run a single pass
    thread.start();
    barrier.await();
    barrier.await();

    recording.stop();
    List<RecordedEvent> beforeEvents = Events.fromRecording(recording);

    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}
 
Example #16
Source File: VMInfo.java    From vjtools with Apache License 2.0 4 votes vote down vote up
public ThreadMXBean getThreadMXBean() throws IOException {
	return jmxClient.getThreadMXBean();
}
 
Example #17
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);
}
 
Example #18
Source File: ServiceThread.java    From HolandaCatalinaFw with Apache License 2.0 4 votes vote down vote up
public final Long getAccumulatedAllocatedMemory() {
    return ((ThreadMXBean)ManagementFactory.getThreadMXBean()).
            getThreadAllocatedBytes(Thread.currentThread().getId()) - getInitialAllocatedMemory();
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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);
}