How to dynamically monitor Java heap size?

96,267

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
96,267
flyingfromchina
Author by

flyingfromchina

Updated on November 17, 2021

Comments

  • flyingfromchina
    flyingfromchina over 2 years

    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

  • rc1
    rc1 about 11 years
    An old thread, but commenting anyway. VisualVM is easy to use and meets my thread programming needs well. thanks for the advice.