09 Sep 6
Unlike C/C++ or older languages, Java helps user in managing their memory and auto release the unused memory.
This process is named “Garbage Collection” (GC). In brief, GC consists of three steps:
1. Mark — Find all the unreachable memory, null-ed variables’ memory and mark.
2. Sweep — Clean-up all those marked memory.
3. Compact — Rearrange the data in the memory so as to provide more constiguous spaces.
In Java, there provides 6 algorithm of GC:
For Young Sections:
1. Plain copying (DEFAULT)
2. Parallel copying
3. Parallel scavenging
For Old Memory Sections:
1. Plain mark-sweep (DEFAULT)
2. Incremental collector
3. Concurrent mark-sweep
(*note that, the heap managed by java divides into three main area: Young — where they will be cleaned very soon; Old — where they will be kept for sometimes; and Perm — where they are not to be cleaned.
If ones’ program is very computational demanding or memory draining, one could inspect and pick the most suitable GC Algorithm.
Profiling Tool
There are actually tools bundled with JDK to help us identify the memory usage and performance of a program.
They are jStat and jConsole (Only bundled with JDK5 or above).
1. To use jStat, you need to provide certain parameter to make it works.
jStat -gc -t <vmid> <interval>
vmid is the GUID for Virtual Machine, while interval states the time between each polling.
*One could make use of jps to locate the JAVA program’s vmid.
2. To use jConsole, it’s simple. Its GUI will lead you thru.