Reputation: 9691
I am trying to monitor the java heap size dynamically. Does anybody know how to get the maximium memory used in the process of running a piece of codes? Does the Runtime.maxMemory()
do the trick? Thanks
Upvotes: 40
Views: 106174
Reputation: 15953
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
Upvotes: 2
Reputation:
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.
Upvotes: 9
Reputation: 311
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
Upvotes: 12
Reputation: 13841
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
Upvotes: 12
Reputation: 14212
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
Upvotes: 32
Reputation: 26
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.
Upvotes: 0
Reputation: 11
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
Upvotes: 1
Reputation: 29680
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.
Upvotes: 4