问题描述
这是我的Java项目的一天内存曲线.如何找出哪个类或方法占用大量内存?
This is a one-day memory curve for my Java project.How do I find out which class or method USES a lot of memory?
推荐答案
您需要可以执行 JVM分配分析.
Java Flight Recorder(JFR)可以帮助您进行低开销的分配分析( 1 , 2 -较旧版本的文档,用户界面现在看起来已经大不相同,但分配"和"TLAB"仍然是用来查找内容的词).确保在您使用的设置中启用了分配分析 .在OpenJDK 11之前,这是一项商业功能.他们说,如今其他一些工具也可以以较低的开销实现此功能(例如异步配置文件).
Java Flight Recorder (JFR) can help you with low overhead allocation profiling (1, 2 — docs for older versions, the UI looks quite different now, but "Allocations" and "TLAB" are still the words used to find stuff). Make sure that allocation profiling is enabled in the settings you use. It's a commercial feature until OpenJDK 11. They say some other tools can do it with low overhead these days too (e.g. async-profiler).
如果您不关心自己的应用程序变得异常缓慢并且运行在JDK上,则< 11(?),VisualVM过去能够向您显示堆栈分配的痕迹.我无法在最新版本(到目前为止为1.4.2)中找到它,但是可以在Oracle JDK 8附带的JVisualVM中找到它. 分析器">检查设置">内存设置">检查记录分配堆栈跟踪".开始分析,等待,等待,等待,确定最大的分配,右键单击获取快照并显示分配堆栈跟踪".看起来YourKit 基本相同,以及JProfiler(他们称之为分配记录").再说一次:大大降低了应用程序的速度,所以不要在产品中使用它.
If you don't care about your app becoming unusably slow and are on JDK < 11(?), VisualVM used to be able to show you stack traces of allocations. I can't find it in the latest version (1.4.2 as of now), but can find it in JVisualVM shipped with my Oracle JDK 8-something. "Profiler" > check "Settings" > "Memory Settings" > check "Record allocation stack traces". Start profiling, wait, wait, wait, identify the biggest allocations, right-click, "Take Snapshot and Show Allocation Stack Traces". It looks like YourKit does basically the same, as well as JProfiler (they call it "allocation recording"). Again: slows down the application a lot, so don't use it in prod.
这篇关于Java,我如何找出哪个类或方法使用大量内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!