How to dynamically monitor Java heap size?

flyingfromchina picture flyingfromchina · Jan 29, 2010 · Viewed 92.1k times · Source

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

Answer

Jacob Schoen picture Jacob Schoen · Jan 29, 2010

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