转载请注明出处:
最近在环境中定位服务问题,由于服务使用的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信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论