问题
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
回答1:
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
回答2:
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.
回答3:
If you like you can visually view a lot of values profiling your app with JConsole.
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
回答4:
jstat -gc <pid> <time> <amount>
jstat -gc `jps -l | grep weblogic\.Server | awk {'print $1'}` 1000 3
3 samples 1 one second see more here
回答5:
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.
回答6:
There is also the java.lang.management package. Use the ManagementFactory to get an MemoryMXBean
instance. It has methods to return a heap and a non-heap memory usage snapshot.
回答7:
Maybe jvmtop is worth a look. It's a command-line tool which provides a live-view at several metrics, including heap size:
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
回答8:
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.
http://www.opnet.com/solutions/application_performance/appinternals-xpert.html
回答9:
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.
来源:https://stackoverflow.com/questions/2163228/how-to-dynamically-monitor-java-heap-size