欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

JVM 调优实战--一个案例理解常用工具(命令)

发布时间:2025/1/21 编程问答 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JVM 调优实战--一个案例理解常用工具(命令) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

测试代码

import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;/*** 从数据库中读取信用数据,套用模型,并把结果进行记录和传输*/public class T15_FullGC_Problem01 {private static class CardInfo {BigDecimal price = new BigDecimal(0.0);String name = "张三";int age = 5;Date birthdate = new Date();public void m() {}}private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50,new ThreadPoolExecutor.DiscardOldestPolicy());public static void main(String[] args) throws Exception {executor.setMaximumPoolSize(50);for (;;){modelFit();Thread.sleep(100);}}private static void modelFit(){List<CardInfo> taskList = getAllCardInfo();taskList.forEach(info -> {// do somethingexecutor.scheduleWithFixedDelay(() -> {//do sth with infoinfo.m();}, 2, 3, TimeUnit.SECONDS);});}private static List<CardInfo> getAllCardInfo(){List<CardInfo> taskList = new ArrayList<>();for (int i = 0; i < 100; i++) {CardInfo ci = new CardInfo();taskList.add(ci);}return taskList;} }
  • java -Xms200M -Xmx200M com.mashibing.jvm.gc.T15_FullGC_Problem01

  • top命令观察到问题:内存不断增长 CPU占用率居高不下

  • jps定位具体java进程

  • jinfo pid

  • jstat -gc 动态观察gc情况 / 阅读GC日志发现频繁GC / arthas观察 / jconsole jstat -gc 4655 500 : 每个500个毫秒打印GC的情况

  • jmap - histo 4655 | head -20,查找有多少对象产生

  • jmap -dump:format=b,file=xxx pid / jmap -histo

  • java -Xms20M -Xmx20M -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError com.mashibing.jvm.gc.T15_FullGC_Problem01

  • 使用MAT / jhat进行dump文件分析 https://www.cnblogs.com/baihuitestsoftware/articles/6406271.html

  • 找到代码的问题

  • 总结

    以上是生活随笔为你收集整理的JVM 调优实战--一个案例理解常用工具(命令)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。