views:

1024

answers:

6

Hi, 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

+4  A: 

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.

Hank Gay
+1 for VisualVM
Mark
+3  A: 

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

jschoen
+4  A: 

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

http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

Start your application with:

-Dcom.sun.management.jmxremote

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

helios
A: 

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.

Kees Jan Koster
A: 

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.

Carlos Heuberger
A: 

jstat -jc

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

3 samples 1 one second see more here

agonen