如何动态监控Java堆大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2163228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:16:37  来源:igfitidea点击:

How to dynamically monitor Java heap size?

javamonitoringheap

提问by flyingfromchina

I am trying to monitor the java heap size dynamically. Does anybody know how to get the maxmium memory used in the process of running a piece of codes? Does the Runtime.maxMemory() do the trick? Thanks

我正在尝试动态监视 Java 堆大小。有人知道如何获得运行一段代码的过程中使用的最大内存吗?Runtime.maxMemory() 能解决问题吗?谢谢

采纳答案by Jacob Schoen

maxMemory()returns the maximum amount of memory that java will use. So That will not get you what you want. totalMemory()is what you are looking for though. See The docs

maxMemory()返回 java 将使用的最大内存量。所以那不会让你得到你想要的。totalMemory()是你正在寻找的东西。请参阅文档

回答by Hank Gay

There are a large number of profiler tools available that should help you with this. A popular commercial tool is YourKit, and it gets rave reviews. A free alternative is VisualVM, which I've used in the past and can provide a lot of insight.

有大量可用的分析器工具可以帮助您解决这个问题。一个流行的商业工具是YourKit,它得到了热烈的评价。一个免费的替代方法是VisualVM,我过去使用过它,可以提供很多见解。

回答by helios

If you like you can visually view a lot of values profiling your app with JConsole.

如果您愿意,您可以使用 JConsole 直观地查看许多分析您的应用程序的值。

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jconsole.html

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jconsole.html

Start your application with:

开始您的应用程序:

-Dcom.sun.management.jmxremote

and you app will be available for select when you start /bin/jconsole.exe

当您启动 /bin/jconsole.exe 时,您的应用程序将可供选择

回答by Kees Jan Koster

Yet another free alternative is to use Java-monitor. Have a look at this live demo. Just click on any of the servers to see detailed graphs on heap memory, non-heap memory, file descriptors, database pools and much more.

另一种免费的替代方法是使用Java-monitor。看看这个现场演示。只需单击任何服务器即可查看有关堆内存、非堆内存、文件描述符、数据库池等的详细图表。

回答by user85421

There is also the java.lang.managementpackage. Use the ManagementFactoryto get an MemoryMXBeaninstance. It has methods to return a heap and a non-heap memory usage snapshot.

还有java.lang.management包。使用ManagementFactory获取MemoryMXBean实例。它具有返回堆和非堆内存使用情况快照的方法。

回答by agonen

jstat -gc <pid> <time> <amount>

jstat -gc `jps -l | grep weblogic\.Server | awk {'print '}` 1000 3

3 samples 1 one second see more here

3 个样本 1 个一秒 在这里查看更多

回答by pschiffer

We use app internals xpert by OpNet to monitor heap usage and leaks in realtime in our load testing environment and production. It's lightweight enough to not impact prod, so we get great data we can't get from QA. We also do profiling of methods and db calls in both environments to help us figure out what code/sql to optimize. Very cool stuff with pretty trend charts, but not free by any stretch. If you've got a lot of dollars riding on your app, it's worth the investment.

我们使用 OpNet 的应用程序内部 xpert 来实时监控我们的负载测试环境和生产中的堆使用情况和泄漏。它足够轻量级,不会影响生产,因此我们可以获得无法从 QA 中获得的大量数据。我们还对两种环境中的方法和数据库调用进行了分析,以帮助我们找出要优化的代码/sql。非常酷的东西,有漂亮的趋势图,但不是免费的。如果您在应用程序上投入了大量资金,那么投资是值得的。

http://www.opnet.com/solutions/application_performance/appinternals-xpert.html

http://www.opnet.com/solutions/application_performance/appinternals-xpert.html

回答by MRalwasser

Maybe jvmtopis worth a look. It's a command-line tool which provides a live-view at several metrics, including heap size:

也许jvmtop值得一看。它是一个命令行工具,可提供多个指标的实时视图,包括堆大小:

 JvmTop 0.4.1 alpha amd64  8 cpus, Linux 2.6.32-27, load avg 0.12
 http://code.google.com/p/jvmtop

  PID MAIN-CLASS      HPCUR HPMAX NHCUR NHMAX    CPU     GC    VM USERNAME   #T DL
 3370 rapperSimpleApp  165m  455m  109m  176m  0.12%  0.00% S6U37 web        21
27338 WatchdogManager   11m   28m   23m  130m  0.00%  0.00% S6U37 web        31
19187 m.jvmtop.JvmTop   20m 3544m   13m  130m  0.93%  0.47% S6U37 web        20
16733 artup.Bootstrap  159m  455m  166m  304m  0.12%  0.00% S6U37 web        46

回答by MRalwasser

I'd also like to add that jmap -heap <PID>does the trick; that is assuming you're an ops guy and need to know how much heap the Java process is using. I cant tell if your question is programmatical, or operational.

我还想补充一点jmap -heap <PID>:那是假设您是一名运维人员并且需要知道 Java 进程使用了​​多少堆。我不知道你的问题是程序化的还是操作性的。