申请ID:马锦龙
1.申请ID:马锦龙2.邮箱:mj2012225@outlook.com
3.原创技术文章
基于JavaAgent的全链路监控四JVM内存与GC信息
除了监控java方法的执行耗时,我们还需要获取应用实例的jvm内存与gc信息,以实时把控我们的服务器性能是否在安全范围。监控jvm内存与gc信息是非常重要的,尤其是在大促以及微博火热爆点的时候,我们需要根据监控信息进行扩容,以保证系统稳定。环境准备
1、IntelliJ IDEA Community Edition
2、jdk1.8.0_45 64位配置信息(路径相关修改为自己的)
1、配置位置:Run/Debug Configurations -> VM options
2、配置内容:-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.MFManifest-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: 192MBmax: 2708MB used: 5MB committed: 184MB use rate: 3%
init: 2MB max: 0MB used: 5MB committed: 7MBuse rate: 75%
name: PS Scavenge count:1 took:2pool name:
name: PS MarkSweep count:0 took:0pool name:
===================================================================================================
init: 192MBmax: 2708MB used: 249MB committed: 624MB use rate: 39%
init: 2MB max: 0MB used: 6MB committed: 7MBuse rate: 78%
name: PS Scavenge count:32 took:98 pool name:
name: PS MarkSweep count:0 took:0pool name:
===================================================================================================
Process finished with exit code -1求通过 这个网上很多地方都转载的文章是你原创的吗? Hmily 发表于 2022-1-24 10:40
这个网上很多地方都转载的文章是你原创的吗?
是的,这篇文章是我的 游客 111.225.254.x 发表于 2022-1-25 12:02
是的,这篇文章是我的
原文发在哪里?在原文地方发一个信息说明是本人发的本贴申请账号,然后回复我。 Hmily 发表于 2022-1-25 17:20
原文发在哪里?在原文地方发一个信息说明是本人发的本贴申请账号,然后回复我。
我如何证明 游客 111.225.254.x 发表于 2022-1-26 07:43
我如何证明
看不懂吗?这个文章原文发在哪里的?
页:
[1]