【arthas诊断CPU和内存问题实战】thread -n 5 + cpu火焰图 +内存火焰图

本文详细描述了如何通过查看线程信息、执行代码分析和使用火焰图来定位Java应用程序中占用CPU和内存最多的部分,特别关注了`AbstractSceneManager`中的`tick`方法和内存中的`HashMap`对象。
摘要由CSDN通过智能技术生成

通过线程信息分析CPU

1.查看线程信息

step1: 先查看哪个线程占比cpu最高

分析:

可与看出 SceneWorker占比最高,但是是哪个类中哪个方法则不是太清楚。

我们还需要去分析代码:

step2.分析代码

1.AbstractSceneManager的 this.sceneWorkers = new SceneExecutor("SceneWorker", sceneWorkerNum); 创建,接下来我们这个线程看下在哪里调用
2.AbstractSceneManager的 execute调用 // 里面发现了sleep方法,其实也就是服务器的帧率了. < 100ms不打印,超过则打印
    这个execute是在run方法体
        在Application的:
            bigWorldSceneManager = new BigWorldSceneManager(1, 8);
            bigWorldSceneManager.start();

3.看下execute做了什么事:
    // 处理场景tick更新
    sceneWorkers.forEach(scenes.values(), scene -> scene.tick(now), false);

    分析:
        这个scene是BigWorldScene。 负责这个沙盘的更新。也就是: AbstraceScene的tick方法,我们进来查看



4.[arthas@18936]$ trace com.elex.icefire.scene.AbstractScene tick -n 10
Press Q or Ctrl+C to abort.
Affect(class count: 2 , method count: 2) cost in 255 ms, listenerId: 6
`---ts=2024-01-24 10:48:25;thread_name=SceneWorker-thread-1;id=112;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@7aec35a
    `---[29.621905ms] com.elex.icefire.scene.BigWorldScene:tick()
        `---[99.50% 29.472456ms ] com.elex.icefire.scene.AbstractScene:tick() #82
            `---[99.88% 29.437892ms ] com.elex.icefire.scene.AbstractScene:tick()
                +---[0.19% 0.055634ms ] com.elex.icefire.scene.event.SceneEventManager:onTiming() #336
                +---[0.01% 0.003456ms ] com.elex.icefire.scene.event.SceneEventManager:onTiming() #338
                +---[0.06% 0.017392ms ] com.elex.icefire.scene.object.SceneObjectContainer:getAll() #340
                `---[99.11% min=0.004368ms,max=13.884755ms,total=29.177286ms,count=5] com.elex.icefire.scene.ticker.ISceneObjectTicker:updateObjects() #366

因此最终使用trace去定位到底哪行代码占用cpu最长

通过火焰图分析CPU

分析:

        很清晰看出来:是心跳占用时间长,我们可以结合trace进一步分析到底哪个类中哪个方法占用时间长。

通过火焰图分析内存

1.开始内存火焰图采样

profiler start -e alloc

2.输出火焰图采样html

profiler stop --format html

3.输出下profiler状态

profiler status

4.打开火焰图

分析

        横向:表示执行时间占比。

        竖向:代表栈调用深度(这个我不太理解)

        看下蓝色的就知道哪个对象最多,目前看出来是: HashMap啥的。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值