tags:

views:

257

answers:

1

I have a requirement to collect statistics of a JVM in regular time intervals using. Later, i need to log the JVM available memory/free memory, used memory, total memory.

Following are the two options i have thought of.

  1. ManagementFactory.getMemoryPoolMXBeans() and iterating through each MemoryPoolMXBean and finally collecting the statistics using MemoryPoolMXBean.getUsage().

  2. Runtime.freeMemory(), Runtime.maxMemory(), Runtime.totalMemory

Which is the best way to collect the statistics. using MemoryPoolMXBean.getUsage() or Runtime Class.

+1  A: 

This is about listening to your memory usage during the all application lifecycle instead of poking for information at various places within your application.

As mentioned in Memory notifications in Java, you have an interactive way to monitor memory since you can add a notification Listener to your MemoryMXBean.

So for punctial very local check, Runtime may be enough, for global monitoring, MemoryMXBeans is the way to go.

VonC