How to Monitor Memory of Java Applications

TermsMax Memory: This is the max memory the Java Application can ever use. This is same as -Xmx java parameter.Total OR Heap Memory: This is the memory that Java Application has already allocated to itself. It may not be using it all yet.Used Memory: This is the Memory that Java Application is ‘actually’ using.Free Memory: This is Heap Memory ‘minus’ Used Memory.

Confused? OK, let us give it one more try:
java -Xms128m -Xmx256m

In this case
Max memory is 256MB.
Initial Heap Size is 128 MB.
Used Memory is what actual java object instances occupy. Let us say it is 48 MB.
Free Memory then is 128 minus 48, so 80 MB.

Remember that
Once Used Memory inches closer to Heap Size, java will automatically increase the Heap Size; this is always done in Chunks for optimization purposes. For eg: After 128 MB, java might decide to increase Heap Size to 160 MB. And so on, until it reaches Max Memory.

You get Out of Memory Exception when no more Heap Memory can be allocated.

How to Monitor the memory usage
If you are running a Application Server like JBoss, it is easy to monitor memory, thread usage etc. using JMX.

Use a Tool like Java Visual VM (its in your JDK_HOME\bin dir) to connect to JMX port of your JBoss AppServer to view a variety of statistics like Memory, Threads in real-time.