How to dynamically monitor Java heap size?

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


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


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.