layout | category | title | tagline | tag | excerpt | lock | ||
---|---|---|---|---|---|---|---|---|
post |
itstack-demo-agent |
四、JVM内存与GC信息 |
by 付政委 |
|
除了监控java方法的执行耗时,我们还需要获取应用实例的jvm内存与gc信息,以实时把控我们的服务器性能是否在安全范围。监控jvm内存与gc信息是非常重要的,尤其是在大促以及微博火热爆点的时候,我们需要根据监控信息进行扩容,以保证系统稳定。 |
need |
作者:小傅哥
博客:https://bugstack.cn
沉淀、分享、成长,让自己和他人都能有所收获!
除了监控java方法的执行耗时,我们还需要获取应用实例的jvm内存与gc信息,以实时把控我们的服务器性能是否在安全范围。监控jvm内存与gc信息是非常重要的,尤其是在大促以及微博火热爆点的时候,我们需要根据监控信息进行扩容,以保证系统稳定。
- IntelliJ IDEA Community Edition
- jdk1.8.0_45 64位
- 配置位置:Run/Debug Configurations -> VM options
- 配置内容:-javaagent:E:\itstack\GIT\itstack.org\itstack-demo-agent\itstack-demo-agent-04\target\itstack-demo-agent-04-1.0.0-SNAPSHOT.jar=testargs
itstack-demo-agent-04
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── org.itstack.demo.agent
│ │ ├── JvmStack.java
│ │ └── MyAgent.java
│ └── resources
│ └── META-INF
│ └── MANIFEST.MF
└── test
└── java
└── org.itstack.demo.test
└── ApiTest.java
JvmStack.java
/**
* 博客:http://itstack.org
* 论坛:http://bugstack.cn
* 公众号:bugstack虫洞栈 {获取学习源码}
* create by fuzhengwei on 2019
*/
class JvmStack {
private static final long MB = 1048576L;
static void printMemoryInfo() {
MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
MemoryUsage headMemory = memory.getHeapMemoryUsage();
String info = String.format("\ninit: %s\t max: %s\t used: %s\t committed: %s\t use rate: %s\n",
headMemory.getInit() / MB + "MB",
headMemory.getMax() / MB + "MB", headMemory.getUsed() / MB + "MB",
headMemory.getCommitted() / MB + "MB",
headMemory.getUsed() * 100 / headMemory.getCommitted() + "%"
);
System.out.print(info);
MemoryUsage nonheadMemory = memory.getNonHeapMemoryUsage();
info = String.format("init: %s\t max: %s\t used: %s\t committed: %s\t use rate: %s\n",
nonheadMemory.getInit() / MB + "MB",
nonheadMemory.getMax() / MB + "MB", nonheadMemory.getUsed() / MB + "MB",
nonheadMemory.getCommitted() / MB + "MB",
nonheadMemory.getUsed() * 100 / nonheadMemory.getCommitted() + "%"
);
System.out.println(info);
}
static void printGCInfo() {
List<GarbageCollectorMXBean> garbages = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean garbage : garbages) {
String info = String.format("name: %s\t count:%s\t took:%s\t pool name:%s",
garbage.getName(),
garbage.getCollectionCount(),
garbage.getCollectionTime(),
Arrays.deepToString(garbage.getMemoryPoolNames()));
System.out.println(info);
}
}
}
MyAgent.java
/**
* 博客:http://itstack.org
* 论坛:http://bugstack.cn
* 公众号:bugstack虫洞栈 {获取学习源码}
* create by fuzhengwei on 2019
*/
public class MyAgent {
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("this is my agent:" + agentArgs);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
public void run() {
JvmStack.printMemoryInfo();
JvmStack.printGCInfo();
System.out.println("===================================================================================================");
}
}, 0, 5000, TimeUnit.MILLISECONDS);
}
}
MANIFEST.MF
Manifest-Version: 1.0
Premain-Class: org.itstack.demo.agent.MyAgent
Can-Redefine-Classes: true
ApiTest.java
/**
* 博客:http://itstack.org
* 论坛:http://bugstack.cn
* 公众号:bugstack虫洞栈 {获取学习源码}
* create by fuzhengwei on 2019
* -javaagent:E:\itstack\GIT\itstack.org\itstack-demo-agent\itstack-demo-agent-04\target\itstack-demo-agent-04-1.0.0-SNAPSHOT.jar=testargs
*/
public class ApiTest {
public static void main(String[] args) {
while (true) {
List<Object> list = new LinkedList<>();
list.add("嗨!JavaAgent");
list.add("嗨!JavaAgent");
list.add("嗨!JavaAgent");
}
}
}
this is my agent:testargs
init: 192MB max: 2708MB used: 5MB committed: 184MB use rate: 3%
init: 2MB max: 0MB used: 5MB committed: 7MB use rate: 75%
name: PS Scavenge count:1 took:2 pool name:[PS Eden Space, PS Survivor Space]
name: PS MarkSweep count:0 took:0 pool name:[PS Eden Space, PS Survivor Space, PS Old Gen]
===================================================================================================
init: 192MB max: 2708MB used: 249MB committed: 624MB use rate: 39%
init: 2MB max: 0MB used: 6MB committed: 7MB use rate: 78%
name: PS Scavenge count:32 took:98 pool name:[PS Eden Space, PS Survivor Space]
name: PS MarkSweep count:0 took:0 pool name:[PS Eden Space, PS Survivor Space, PS Old Gen]
===================================================================================================
Process finished with exit code -1
微信搜索「bugstack虫洞栈」公众号,关注后回复「基于JavaAgent的全链路监控」获取本文源码&更多原创专题案例!