Diagram for Java Memory Structure and Garbage Collection

All Memory for JVM:

  • Heap Memory, which is the storage for Java objects
  • Non-Heap Memory, which is used by Java to store loaded classes and other meta-data, String Pool, etc.
  • JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.

When JVM starts, it reserves memory for the Heap. Sometime, not all the memory is used at the beginning. But it will not be used by others for sure. You can use -Xmx parameter to set the max JVM memory size and -Xms for initial memory size.

By default:

  • If empty heap size < 40%, JVM will increase heap size to -Xmx size.
  • If empty heap size > 70%, JVM will decrease heap size to -Xms size.

You also can use -XX:MaxPermSize to set Non-Heap memory size.

Location for Object at different age

Big version: http://bit.ly/Hy1Fku

If -Xms is smaller than -Xmx, the difference is the Virtual part. With programs running, Yound, Tenured and Perm will gradually use this part.

How Age Algorithm works?

In JVM memory model, there are two parts in Heap. One is New Generation and the other is Pld Generation. In New Generation, there is Eden part for new Object. there are two Survivor Spaces(from and to), which are alwayse the same size. They are used to store survival Objects after GC(Garbarge Collection). In Old Generation, long live time Objects stay there.

In New Generation, GC normally use Copying Algorithm, which is fast. Everytime, survival Objects are copied to one of the Survivor Space. If Survivor Space is full, rest live Objects are directly copoed to Old Generation. So, after GC, Eden memory will be cleaned up. In Old Generation, GC nornally use Mark-Compact Algorithm, which is slow but requires less memory.

There are several level of GC, 0 level is Full, which clean garbage in OLD Generation, 1 or above levels are partial GC, which clean New Generation garbage. Out of Memory will happen where is no space for new Object even GC done for OLD or Perm part.

How to claim memory?

  1. JVM tries to find space in Eden
  2. if there is enough space, memory claim is done. Otherwise, go to next step
  3. JVM tries to release not active objects (1 or higher level GC). If there is still not enough space, put active objects into Survivor.
  4. Survivor part is used as exchange space for Eden and Old. If Old space is big enough, objects in Survivor will be moved into Old space.
  5. if Old part does not have enough space, JVm will perform full GC (0 level)
  6. after full GC, if Survivor and Old parts still can not hold the objects from Eden, there is the “Out of Memory Error”.

Object move during GC: eden -> survivor -> tenured

There is a Chinese version: http://blog.csdn.net/autofei/article/details/7456213


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s