当前位置: 代码网 > it编程>编程语言>Java > 如何使用java代码获取JVM信息

如何使用java代码获取JVM信息

2025年04月14日 Java 我要评论
转载请注明出处:最近在环境中定位服务问题,由于服务使用的docker部署的,且使用的docker镜像,在启动之后,容器内没有jdk相关的工具【jstat、jmap等等】;于是采用 在项目中使用java

转载请注明出处:

最近在环境中定位服务问题,由于服务使用的docker部署的,且使用的docker镜像,在启动之后,容器内没有jdk相关的工具【jstat、jmap等等】;于是采用 在项目中使用java类获取jvm相关信息,以下是测试的示例:

import java.lang.management.managementfactory;  
import java.lang.management.memorymxbean;  
import java.lang.management.memoryusage;  
import java.lang.management.garbagecollectormxbean;  
import java.lang.management.memorypoolmxbean;  
import java.lang.management.threadmxbean;  
import java.util.list;  
public class gcinfodemo {  
    public static void main(string[] args) {  
        // 获取内存管理 bean  
        memorymxbean memorymxbean = managementfactory.getmemorymxbean();  
        memoryusage heapmemoryusage = memorymxbean.getheapmemoryusage();  
        memoryusage nonheapmemoryusage = memorymxbean.getnonheapmemoryusage();  
        // 打印内存信息  
        system.out.println("heap memory usage:");  
        system.out.println("  init: " + heapmemoryusage.getinit() / (1024 * 1024) + " mb");  
        system.out.println("  used: " + heapmemoryusage.getused() / (1024 * 1024) + " mb");  
        system.out.println("  committed: " + heapmemoryusage.getcommitted() / (1024 * 1024) + " mb");  
        system.out.println("  max: " + heapmemoryusage.getmax() / (1024 * 1024) + " mb");  
        system.out.println("\nnon-heap memory usage:");  
        system.out.println("  init: " + nonheapmemoryusage.getinit() / (1024 * 1024) + " mb");  
        system.out.println("  used: " + nonheapmemoryusage.getused() / (1024 * 1024) + " mb");  
        system.out.println("  committed: " + nonheapmemoryusage.getcommitted() / (1024 * 1024) + " mb");  
        system.out.println("  max: " + nonheapmemoryusage.getmax() / (1024 * 1024) + " mb");  
        // 获取垃圾回收回收器 bean 的列表  
        list<garbagecollectormxbean> gcbeans = managementfactory.getgarbagecollectormxbeans();  
        system.out.println("\ngarbage collectors:");  
        for (garbagecollectormxbean gcbean : gcbeans) {  
            system.out.println("  name: " + gcbean.getname());  
            system.out.println("  number of collections: " + gcbean.getcollectioncount());  
            system.out.println("  total time spent in collections: " + gcbean.getcollectiontime() + " ms");  
        }  
        // 打印内存池信息  
        system.out.println("\nmemory pools:");  
        for (memorypoolmxbean memorypool : managementfactory.getmemorypoolmxbeans()) {  
            system.out.println("  name: " + memorypool.getname());  
            system.out.println("  usage: " + memorypool.getusage());  
        }  
        // 获取线程管理 bean  
        threadmxbean threadmxbean = managementfactory.getthreadmxbean();  
        // 获取所有线程 id  
        long[] threadids = threadmxbean.getallthreadids();  
        system.out.println("\nthreads information:");  
        for (long threadid : threadids) {  
            system.out.println("  thread id: " + threadid);  
            system.out.println("  thread name: " + threadmxbean.getthreadinfo(threadid).getthreadname());  
            system.out.println("  thread state: " + threadmxbean.getthreadinfo(threadid).getthreadstate());  
        }  
        // 检查死锁线程  
        long[] deadlockedthreads = threadmxbean.finddeadlockedthreads();  
        if (deadlockedthreads != null) {  
            system.out.println("\ndeadlocked threads:");  
            for (long deadlockedthreadid : deadlockedthreads) {  
                system.out.println("  deadlocked thread id: " + deadlockedthreadid);  
                system.out.println("  thread name: " + threadmxbean.getthreadinfo(deadlockedthreadid).getthreadname());  
            }  
        } else {  
            system.out.println("\nno deadlocked threads found.");  
        }  
    }  
}
  • 获取gc信息

    • 通过managementfactory.getgarbagecollectormxbeans()获取所有垃圾回收器的监控bean。

    • 每个garbagecollectormxbean提供:

      • getname():gc算法名称(如g1 young generation

      • getcollectioncount():回收次数

      • getcollectiontime():累计耗时(毫秒)

2.打印当前线程信息:

    • 使用 threadmxbean 获取当前线程的所有 id,并通过 getthreadinfo 方法获取每个线程的信息,包括线程名字和状态。

3.检查死锁线程:

    • 使用 finddeadlockedthreads 方法检查 jvm 中的死锁情况。如果有死锁线程,则输出这些线程的 id 和名称。如果没有,则输出相应的消息。
heap memory usage:
  init: 508 mb
  used: 10 mb
  committed: 487 mb
  max: 7205 mb
non-heap memory usage:
  init: 2 mb
  used: 4 mb
  committed: 7 mb
  max: 0 mb
garbage collectors:
  name: ps scavenge
  number of collections: 0
  total time spent in collections: 0 ms
  name: ps marksweep
  number of collections: 0
  total time spent in collections: 0 ms
memory pools:
  name: code cache
  usage: init = 2555904(2496k) used = 1235968(1207k) committed = 2555904(2496k) max = 251658240(245760k)
  name: metaspace
  usage: init = 0(0k) used = 3597432(3513k) committed = 4980736(4864k) max = -1(-1k)
  name: compressed class space
  usage: init = 0(0k) used = 392584(383k) committed = 524288(512k) max = 1073741824(1048576k)
  name: ps eden space
  usage: init = 133169152(130048k) used = 10914704(10658k) committed = 133169152(130048k) max = 2789212160(2723840k)
  name: ps survivor space
  usage: init = 22020096(21504k) used = 0(0k) committed = 22020096(21504k) max = 22020096(21504k)
  name: ps old gen
  usage: init = 355467264(347136k) used = 0(0k) committed = 355467264(347136k) max = 5666504704(5533696k)
threads information:
  thread id: 6
  thread name: monitor ctrl-break
  thread state: runnable
  thread id: 5
  thread name: attach listener
  thread state: runnable
  thread id: 4
  thread name: signal dispatcher
  thread state: runnable
  thread id: 3
  thread name: finalizer
  thread state: waiting
  thread id: 2
  thread name: reference handler
  thread state: waiting
  thread id: 1
  thread name: main
  thread state: runnable
no deadlocked threads found.
process finished with exit code 0

到此这篇关于使用java代码获取jvm信息的文章就介绍到这了,更多相关使用java代码获取jvm信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com