How to dynamically monitor Java heap size?

Viewed 97743

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

9 Answers

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.

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.

Related