2012年5月30日 星期三

Garbage Collehttp://www.blogger.com/blogger.g?blogID=5091259175692372980#editor/target=post;postID=7559320302580085586ction (2)

Let me introduce some of the key words before going to Java practical GC tuning skills:


1. Stop-the-world
It simply halt the program and trace the garbage using Tracing Collectors.

2. Incremental
Incremental GC is designed to reduce this halting time of 'Stop-the-world'. It separates the GC cycle in discrete phases.

3. Concurrent
Concurrent GC does not the program execution at all.


4 .Compacting -
 After discarding the non-reachable objects,it may copy some or all of the reachable objects into a new area of memory.

5. Non-compacting -Moving may seems inefficient, so there is another approach. It does not copy the objects to the new area of memory.

6. Generational GC- It divides the object into generations. In Java, it divides the heap space into 5 area, namely eden, survivor, survivor2 , tenured and permanent area.
At first, objects are allocated to Eden and Survivor 1 and 2 would collect objects on each of the minor GC. If they survive in the survivor 1 or 2 long enough, these objects are copied to  Tenured area.


(Not Done)



Reference from:
http://blog.griddynamics.com/2011/06/understanding-gc-pauses-in-jvm-hotspots.html
(This is really a good page learning the mathematics of GC)

Garbage Collection(1)

Garbage Collection is a essential feature of java.

Having gone through a lot of pages in the web, I have now an idea and would like to share to you guys.



There are mainly 2 types of GC, they are
  • Reference counting collectors

  • Tracing garbage collectors
 Finding out the root of the object(A reachable object) through the whole memory
 and traversing the entire root set.The reachable object are marked are being 'in-use'
 Not 'in-use' object 's memory would be cleared in every stage of Garbage Collection.

Ananology:
In an untidied room, there are some usable things and some rubbish in every corners of the room. We find out the usable object and mark them as usable and throw away all non-marked items.

We would focus on the tracing garbage collectors, that Java is using.





2012年5月23日 星期三

Memory Leak Detection

Enabling Garbage Collection Log:

The Verbose GC log is defined when the JVM process is started. There are a couple of switches that can be used:
  1. -verbose:gc — prints basic information about GC to the standard output
  2. -XX:+PrintGCTimeStamps — prints the times that GC executes
  3. -XX:+PrintGCDetails — prints statistics about different regions of memory in the JVM
  4. -Xloggc:<file> — logs the results of GC in the given file
Analyse of the heap dump:

Heap dumps are saved on disk, and the files might be fairly large. A good rule is to make sure that you have at least twice the size of the physical memory free on the disk before you initiate a memory dump.

With those final words of caution out of the way, you should now be ready to run the following command:

jmap -heap:live,format=b,file=FILENAME PID

Determing the cause of the memory leakage:

Using any Heap analysis software, such as jhat in jdk in order to open the heap.

To determine which objects are leaked, sort the classes by total memory usage of all instances of each class. The objects near the top of the list are usually the leaked objects. Then, you can follow the reference chain to them until you find the cause of the memory leak.

A useful heuristic here is that if you sort all the objects by retained heap, you can find the objects that are likely the root cause of the memory leak.

Abstract from http://java.dzone.com/news/how-fix-memory-leaks-java