Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically monitor Java heap size?

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

like image 278
flyingfromchina Avatar asked Jan 29 '10 16:01

flyingfromchina


People also ask

How do I monitor Java heap size?

The easy way to monitor Heap usage is by using a commercial APM (Application Performance management tool) such as CA Wily APM, AppDynamics, New Relic, Riverbed, etc. APM tools not only monitor the heap usage, but you can also configure the tool to Alert you when Heap usage is not normal.

How do I monitor Java memory usage?

Memory-Monitoring Tools Since Java 5, the standard JDK monitoring tool has been JConsole. The Oracle JDK also includes jStat, which enables the monitoring of memory usage and garbage-collector activity from the console, and Java VisualVM (or jvisualvm), which provides rudimentary memory analyzes and a profiler.

Is heap size dynamic?

Heap memory is dynamic, like stack memory, in that it is allocated at run time.

Is Java heap dynamic?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.


2 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

like image 131
Jacob Schoen Avatar answered Sep 16 '22 11:09

Jacob Schoen


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.

like image 29
Hank Gay Avatar answered Sep 17 '22 11:09

Hank Gay