Run Java Application with the following VM Args.
In Eclipse, specify the VM Args in Run/Debug Configurations->Arguments->VM arguments
-verbose:gc -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
-verbose:gc print the garbage collector's work information
-Xms,-Xmx the min/max of the application's heap size
-XX:+HeapDumpOnOutOfMemoryError Run HeamDump when OutOfMemoryError occurs. It will create a .hprof somewhere.
Open the .hprof file which is called the heap dump file using Java Memory Analyze.
Other VM Args and their meaning:
-Xss vm stack size (vm stack is the stack of methods which are runing on jvm)
-Xoss native method stack size(it's the stack of native methods which won't run on jvm)
Some of the vm arguments are specified in eclipse.ini file which is in the root directory of Eclipse by default. Usually the same directory with eclipse.exe.
-vmargs
-Xms40m
-Xmx512m