你能在 Java 中获得基本的 GC 统计信息吗?

编程入门 行业动态 更新时间:2024-10-27 18:20:14
本文介绍了你能在 Java 中获得基本的 GC 统计信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想让一些长时间运行的服务器应用程序定期输出 Java 中的一般 GC 性能数字,比如 Runtime.freeMemory() 等 GC 等值.比如完成的周期数、平均时间等.

I would like to have some long-running server applications periodically output general GC performance numbers in Java, something like the GC equivalent of Runtime.freeMemory(), etc. Something like number of cycles done, average time, etc.

我们的系统在客户机器上运行,怀疑配置错误的内存池会导致过多的 GC 频率和长度 - 我认为定期报告基本 GC 活动通常是好的.

We have systems running on customer machines where there is a suspicion that misconfigured memory pools are causing excessive GC frequency and length - it occurs to me that it would be good in general to periodically report the basic GC activity.

是否有任何独立于平台的方式来做到这一点?

Is there any platform independent way to do this?

我特别想在运行时将此数据输出到系统日志(控制台);这不是我想连接到 JVM 的东西,就像 JConsole 或 JVisualVM 一样.

Edit2:MX bean 看起来像我想要的 - 有没有人有一个获得其中之一的工作代码示例?

The MX bean looks like what I want - does anyone have a working code example which obtains one of these?

推荐答案

这是一个使用 GarbageCollectorMXBean 打印出 GC 统计信息.大概您会定期调用此方法,例如使用 ScheduledExecutorService 进行调度.

Here's an example using GarbageCollectorMXBean to print out GC stats. Presumably you would call this method periodically, e.g. scheduling using a ScheduledExecutorService.

public void printGCStats() { long totalGarbageCollections = 0; long garbageCollectionTime = 0; for(GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) { long count = gc.getCollectionCount(); if(count >= 0) { totalGarbageCollections += count; } long time = gc.getCollectionTime(); if(time >= 0) { garbageCollectionTime += time; } } System.out.println("Total Garbage Collections: " + totalGarbageCollections); System.out.println("Total Garbage Collection Time (ms): " + garbageCollectionTime); }

更多推荐

你能在 Java 中获得基本的 GC 统计信息吗?

本文发布于:2023-06-04 21:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/505237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能在   统计信息   Java   GC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!