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
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
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
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.
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
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.
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.