SlideShare a Scribd company logo
1 of 330
Java Performance
           artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




Garbage Collection
Java Performance
                                                                                     2

                            artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




1) Garbage Collection
2) Garbage Collection Algorithm
3) Hotspot JVM   Garbage Collection

4) IBM JVM   Garbage Collection
Java Performance
                                                                          3
                       Java Performance Fundamental | twitter @novathinker
                                    artdb@ex-em.com | performeister.tistory.com
  Garbage Collection



• Garbage Collection
  – Garbage Collector
  –                     Unreferenced Memory
  –
  – Fragmentation


    “Heap storage for objects is reclaimed by an automatic
    storage management system (typically a garbage
    collector); objects are never explicitly deallocated.”
    - Java Virtual Machine Speculation, Section 3.5.3 [JVMS2
    1999]
Java Performance
                                                                           4
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
   Garbage Collection




     !
Garbage
Collector
Java Performance
                                                                           4
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
   Garbage Collection




     !                                                          object
Garbage
Collector
                                       Heap memory
                                                 .
Java Performance
                                                                           4
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
   Garbage Collection




     !                                                          object
Garbage
Collector
                                       Heap memory
                                                 .


                                               Garbage Collection
                                           Memory Recycling       Heap
                                       Fragmentation                           .
Java Performance
                                                                           4
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
   Garbage Collection




     !                                                            object
Garbage
Collector
                                       Heap memory
                                                 .


                                               Garbage Collection
                                           Memory Recycling       Heap
                                       Fragmentation                              .




 • JVM       heap          new, newarray, anewarray, multianewarray instruction
                                                    instruction
 • Garbage Collection
 • JVM       Spec                   Garbage Collection Algorithm
Java Performance
                                                                        5
                     Java Performance Fundamental | twitter @novathinker
                                  artdb@ex-em.com | performeister.tistory.com
Garbage Collection




                                  !
                             Garbage
                             Collector
Java Performance
                                                                        5
                     Java Performance Fundamental | twitter @novathinker
                                  artdb@ex-em.com | performeister.tistory.com
Garbage Collection




                                                                    Memory


                                  !                 Memory             System
                             Garbage
                             Collector
                                                      crash
                                                                .
Java Performance
                                                                           5
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
   Garbage Collection




                                                                       Memory


                                     !                 Memory             System
                                Garbage
                                Collector
                                                         crash
                                                                   .




Program

CPU Time        Scheduling

     .
Java Performance
                                                                          6
                       Java Performance Fundamental | twitter @novathinker
                                    artdb@ex-em.com | performeister.tistory.com
  Garbage Collection




• Garbage Collection
  – Garbage Collection
Java Performance
                                                                   7
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
Garbage Collection
Java Performance
                                                                   7
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
Garbage Collection




                           ROOT SET
Java Performance
                                                                     7
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Garbage Collection




                             ROOT SET




 Local Variable,
Operand Stack
Java Performance
                                                                     7
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Garbage Collection




                             ROOT SET




 Local Variable,
Operand Stack


                                   Class
                           Constant Pool
                              Reference
Java Performance
                                                                     7
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Garbage Collection




                             ROOT SET




 Local Variable,                                        Unreleased Native
Operand Stack                                               Method
                                                                Object
                                                           Reference
                                   Class
                           Constant Pool
                              Reference
Java Performance
                                                                         8
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
  Garbage Colletion



• Root Set
Java Performance
                                                                         9
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
  Garbage Colletion



• Memory Leak
class Main{
    public static void main (String args[]) {
       Leak lk = new Leak();
       for(int a=0; a<9000000; a++) {
          lk.addList(a);
          lk.removeStr(a);
       }
    }
}

class Leak {
     ArrayList lst = new ArrayList();
     public void addList(int a) {
         lst.add("                            "+a);
     }
     public void removeStr(int i) {
         Object obj = lst.get(i);
         obj = null;
     }
}
Java Performance
                                                                           9
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
  Garbage Colletion



• Memory Leak
class Main{
    public static void main (String args[]) {
       Leak lk = new Leak();
       for(int a=0; a<9000000; a++) {
          lk.addList(a);
          lk.removeStr(a);
       }
    }                                                          Why leak?
}

class Leak {
     ArrayList lst = new ArrayList();
     public void addList(int a) {
         lst.add("                            "+a);
     }
     public void removeStr(int i) {
         Object obj = lst.get(i);
         obj = null;
     }
}
Java Performance
                                                      10
   Java Performance Fundamental | twitter @novathinker
                artdb@ex-em.com | performeister.tistory.com




Garbage Collection
    Algorithm
Java Performance
                                                                                     11

                            artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




Garbage Collection Algorithm
1) Reference Counting Algorithm
2) Mark-and-Sweep Algorithm
3) Mark-and-Compacting Algorithm
4) Copying Algorithm
5) Generational Algorithm
6) Train Algorithm
7) Adaptive Algorithm
Java Performance
                                                                       12
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Garbage Collection Algorithm
Java Performance
                                                                       12
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Garbage Collection Algorithm



                       Garbage Object                     Detection
                             • Live Object : Root Set
                             •               Garbage Object
Java Performance
                                                                       12
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Garbage Collection Algorithm



                       Garbage Object                     Detection
                             • Live Object : Root Set
                             •               Garbage Object




      Garbage Object
             • Heap Memory Reclaim
Java Performance
                                                                         13
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




•   Reference Counting Algorithm
•   Mark-and-Sweep Algorithm
•   Copying Algorithm
•   Mark-and-Compacting Algorithm
•   Generational Algorithm
•   Train Algorithm
•   Adaptive Algorithm
Java Performance
                                                                       14
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Reference Counting Algorithm
  –                                 GC
  –         Object               Reference count
  – Reference Count                      0      GC
  –       Object                  GC        Object
      Object                                 Object           Count
                                         Count
Java Performance
                                                                      15
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm




                                                           Integer


                                                          RefCnt 00




                                                           Integer


                                                          RefCnt 00

                                                           Integer


                                                          RefCnt 00
Java Performance
                                                                       15
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                                            Integer
Object a = new Ingeter(1);

                                                           RefCnt 00




                                                            Integer


                                                           RefCnt 00

                                                            Integer


                                                           RefCnt 00
Java Performance
                                                                       15
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                                            Integer
Object a = new Ingeter(1);               a                     1
                                                           RefCnt 01
                                                                   0




                                                            Integer


                                                           RefCnt 00

                                                            Integer


                                                           RefCnt 00
Java Performance
                                                                       15
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                                            Integer
Object a = new Ingeter(1);               a                     1
Object b = a;                                              RefCnt 01
                                                                   0




                                                            Integer


                                                           RefCnt 00

                                                            Integer


                                                           RefCnt 00
Java Performance
                                                                       15
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                                            Integer
Object a = new Ingeter(1);               a                     1
Object b = a;                            b                 RefCnt 01
                                                                   0
                                                                   2




                                                            Integer


                                                           RefCnt 00

                                                            Integer


                                                           RefCnt 00
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer


Object a = new Ingeter(1);                                  RefCnt 00

                                                             Integer


                                                            RefCnt 00
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer

                                          a                     1
Object a = new Ingeter(1);                                          0
                                                            RefCnt 01
                                                             Integer


                                                            RefCnt 00
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer

                                          a                     1
Object a = new Ingeter(1);                                          0
                                                            RefCnt 01
Object b = new Ingeter(2);                                   Integer


                                                            RefCnt 00
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer

                                          a                     1
Object a = new Ingeter(1);                                          0
                                                            RefCnt 01
Object b = new Ingeter(2);                                   Integer
                                                                2
                                          b                 RefCnt 01
                                                                    0
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer

                                          a                     1
Object a = new Ingeter(1);                                          0
                                                            RefCnt 01
Object b = new Ingeter(2);                                   Integer
a = b;                                                          2
                                          b                 RefCnt 01
                                                                    0
Java Performance
                                                                        15
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                                             Integer
Object a = new Ingeter(1);                a                     1
Object b = a;                             b                 RefCnt 01
                                                                    0
                                                                    2




                                                             Integer

                                          a                     1
Object a = new Ingeter(1);                                          0
                                                            RefCnt 01
Object b = new Ingeter(2);                                   Integer
a = b;                                                          2
                                          b                 RefCnt 01
                                                                    0
                                                                    2
Java Performance
                                                                     16
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));




                                                           Integer
                                                             1
                                TwoInteger
                                                         RefCnt 00
                                                                 1
                                   ref
     a
                                RefCnt 00
                                        1                  Integer
                                                             2
                                                         RefCnt 01
                                                                 0
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));

a = null;



                                                           Integer
                                                             1
                                TwoInteger
                                                         RefCnt 00
                                                                 1
                                   ref
     a
                                RefCnt 00
                                        1                  Integer
                                                             2
                                                         RefCnt 01
                                                                 0
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));

a = null;



                                                           Integer
                                                             1
                                TwoInteger
                                                         RefCnt 00
                                                                 1
                                   ref
     a
                                RefCnt 00
                                        1                  Integer
                                                             2
                                                         RefCnt 01
                                                                 0
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));

a = null;



                                                           Integer
                                                             1
                                TwoInteger
                                                         RefCnt 00
                                                                 1
                                   ref
     a                            GC
                                RefCnt 00
                                        1                  Integer
                                                             2
                                                         RefCnt 01
                                                                 0
Java Performance
                                                                      16
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Object a = new TwoIngeter(new Integer(1), new Integer(2));

a = null;



                                                           Integer
                                                             1
                                                         RefCnt 00
                                                                 1
     a
                                                           Integer
                                                             2
                                                         RefCnt 01
                                                                 0
                                                                 0
Java Performance
                                                                          17
                       Java Performance Fundamental | twitter @novathinker
                                    artdb@ex-em.com | performeister.tistory.com
     Garbage Collection Algorithm



• Reference Counting Algorithm


• Garbage Object
• Garbage
• Garbage Collector                                   (         )




• Reference Count
• Linked List                                     Reference Count   0
              Leak
Java Performance
                                                                           18
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




a


               Linked List
                 next ref
                RefCnt 02

                                      Linked List
                                        next ref
                                       RefCnt 11

                                                             Linked List
                                                              next ref
                                                              RefCnt 01
Java Performance
                                                                           18
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




a


               Linked List
                 next ref
                        2
                RefCnt 01

                                      Linked List
                                        next ref
                                       RefCnt 11

                                                             Linked List
                                                              next ref
                                                              RefCnt 01
Java Performance
                                                                       19
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Mark-and-Sweep Algorithm
  – Root Set                               Reference
  – Tracing Algorithm
  – Mark                         Sweep
       • Mark : Live Object                   Mark
         (Object     flag or                   bitmap table                  )
       • Sweep : Heap                Mark
Java Performance
                                                                          20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




R                          ref                ref                  ref
                            flag              flag                 flag



                           ref                ref                  ref
                            flag              flag                 flag



                           ref                ref                  ref
                             flag             flag                 flag
Java Performance
                                                                          20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




                                    Mark Phase
R                          ref                ref                  ref
                            flag              flag                 flag



                           ref                ref                  ref
                            flag              flag                 flag



                           ref                ref                  ref
                             flag             flag                 flag
Java Performance
                                                                         20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




                                    Mark Phase
R                          ref                ref                 ref
                      
                            flag          
                                              flag             
                                                                flag



                           ref                ref                 ref
                            flag              flag            
                                                                  flag



                           ref                ref                 ref
                             flag         
                                              flag             
                                                                flag
Java Performance
                                                                         20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




                                    Sweep Phase
R                          ref                ref                 ref
                      
                            flag          
                                              flag             
                                                                flag



                           ref                ref                 ref
                            flag              flag            
                                                                  flag



                           ref                ref                 ref
                             flag         
                                              flag             
                                                                flag
Java Performance
                                                                         20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




                                   Sweep Phase
R                          ref                ref                 ref
                      
                            flag          
                                              flag             
                                                                flag



                                                                  ref
                                                              
                                                                  flag



                                              ref                 ref
                                          
                                              flag             
                                                                flag
Java Performance
                                                                         20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




R                          ref                ref                 ref
                      
                            flag          
                                              flag             
                                                                flag



                                                                  ref
                                                              
                                                                  flag



                                              ref                 ref
                                          
                                              flag             
                                                                flag
Java Performance
                                                                          20
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




R                          ref                ref                  ref
                            flag              flag                 flag



                                                                   ref
                                                                   flag



                                              ref                  ref
                                              flag                 flag
Java Performance
                                                                          21
                       Java Performance Fundamental | twitter @novathinker
                                    artdb@ex-em.com | performeister.tistory.com
     Garbage Collection Algorithm




• Mark-and-Sweep Algorithm

• Reference
• Reference                             Overhead




• Suspend
• Fragmentation
          Memory
Java Performance
                                                                       22
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Mark-and-Compacting Algorithm
  – Fragmentation                    Memory
       Algorithm
  – Mark                         Compaction
       • Mark Phase : Live Object               Mark
       • Compaction : Live Object                         Memory


  –                         Handle
       • Mark Phase : Handle               Marking
       • Compaction : Object                     Handle Update
Java Performance
                                                                           23
                        Java Performance Fundamental | twitter @novathinker
                                     artdb@ex-em.com | performeister.tistory.com
      Garbage Collection Algorithm




 • Mark-and-Compacting Algorithm
     – Compaction
                                               Arbitrary                  Worst

                                               4 1 2 3
      1         2               3    4

                                               Linear

                                               1 3 4 2


• Arbitrary :                                  Sliding
• Linear :             Pointer
                                               1 2 3 4
• Sliding : Allocation
                                                                           Best
Java Performance
                                                                       24
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                2

        Handle
0     flag
1     flag        T22                       A     5           Z    1
2     flag        T11
3     flag        T12
                                            B     9          W     7
4     flag
5     flag        T21
6     flag                                  Q     3          C     2
7     flag        T31
8     flag
9     flag        T32
Java Performance
                                                                       24
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                2      Mark Phase
        Handle
0     flag
1     flag        T22                       A     5           Z    1
2     flag        T11
3     flag        T12
                                            B     9          W     7
4     flag
5     flag        T21
6     flag                                  Q     3          C     2
7     flag        T31
8     flag
9     flag        T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2      Mark Phase
         Handle
0      flag
1      flag        T22                       A     5           Z    1
  
2  flag            T11
3      flag        T12
                                             B     9          W     7
4      flag
  
5  flag            T21
6      flag                                  Q     3          C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2      Mark Phase
         Handle
0      flag
1      flag        T22                       A     5           Z    1
  
2  flag            T11
3      flag        T12
                                             B     9          W     7
4      flag
  
5  flag            T21
6      flag                                  Q     3          C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2      Mark Phase
         Handle
0      flag
1      flag        T22                       A     5           Z    1
  
2  flag            T11
3      flag        T12
                                             B     9          W     7
4      flag
  
5  flag            T21
6      flag                                  Q     3          C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2   Compation Phase
         Handle
0      flag
1      flag        T22                       A     5           Z    1
  
2  flag            T11
3      flag        T12
                                             B     9          W     7
4      flag
  
5  flag            T21
6      flag                                  Q     3          C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2   Compation Phase
         Handle
0      flag
1      flag        T22                       A     5
  
2  flag            T11
3      flag        T12
                                             B     9
4      flag
  
5  flag            T21
6      flag                                                   C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2   Compation Phase
         Handle
0      flag
1      flag        T22                       A     5           B    9
  
2  flag            T11
3      flag        T12
4      flag
  
5  flag            T12
                   T21
6      flag                                                   C     2
7      flag        T31
8      flag
  
9  flag            T32
Java Performance
                                                                        24
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




                                 2   Compation Phase
         Handle
0      flag
1      flag        T22                       A     5           B    9
  
2  flag            T11
3      flag        T12
                                              C     2
4      flag
  
5  flag            T12
                   T21
6      flag
7      flag        T31
8      flag
  
9  flag            T21
                   T32
Java Performance
                                                                       24
                   Java Performance Fundamental | twitter @novathinker
                                artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




                                2

        Handle
0     flag
1     flag        T22                       A     5           B    9
2     flag        T11
3     flag        T12
                                             C     2
4     flag
5     flag        T12
                  T21
6     flag
7     flag        T31
8     flag
9     flag        T21
                  T32
Java Performance
                                                                        25
                     Java Performance Fundamental | twitter @novathinker
                                  artdb@ex-em.com | performeister.tistory.com
     Garbage Collection Algorithm




• Mark-and-Compacting Algorithm

• Fragmentation
• Memory




• Reference                    Object   Access    Overhead
• Suspend
Java Performance
                                                                       26
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Copying Algorithm
  – Stop-the-Copy Algorithm
  – Heap                 Active           Inactive

  – Active                        Object
  – Active                               Live Object          Inactive
    Copy
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm



                                         Active Area

                                   A                            C



                                                B



R




                                          Inactive Area
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm



                                          Active Area

                                    A                           C
                                   null                        null

                                                B
                                              null

R




                                          Inactive Area
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm



                                         Active Area

                                   A                            C
                                                               null

                                                B
                                              null

R
                       A’
                      null




                                          Inactive Area
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm



                                          Active Area

                                   A                            C
                                                               null

                                                B



R
                       A’           B’
                      null         null




                                          Inactive Area
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm



                                          Active Area

                                   A                            C



                                                 B



R
                       A’           B’    C’
                      null         null   null




                                           Inactive Area
Java Performance
                                                                         27
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
    Garbage Collection Algorithm




                                          Inactive Area




R
                       A’           B’     C’
                      null         null   null




                                            Active Area
Java Performance
                                                                        28
                     Java Performance Fundamental | twitter @novathinker
    Garbage Collection Algorithm  artdb@ex-em.com | performeister.tistory.com




• Copying Algorithm

• Fragmentation
   Inactive                   Copy




• GC Suspend
• Copy     Overhead
•
Java Performance
                                                                       29
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Generational Algorithm
  – Copying Algorithm
       • Copy
       •                                                        Object

       •
           Object
       • Long Lived Object
  – Heap age              sub heap
    Youngest Generation Sub heap                                   GC
  – Object                                  Age
Java Performance
                                                                     30
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Youngest Generation Sub Heap

  Matured                Live      Live         Dead         Dead

     Live             Matured      Dead         Live         Dead

    Dead                 Live      Dead         Dead       Matured

Tenured Generation Sub Heap
Java Performance
                                                                    30
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Youngest Generation Sub Heap

                         Live      Live         Dead         Dead

     Live                          Dead         Live         Dead

    Dead                 Live      Dead         Dead

Tenured Generation Sub Heap                                 GC

  Matured             Matured    Matured
Java Performance
                                                                    30
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Youngest Generation Sub Heap

                         Live      Live

     Live                                       Live

                         Live

Tenured Generation Sub Heap                                 GC

  Matured             Matured    Matured
Java Performance
                                                                    30
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
 Garbage Collection Algorithm




Youngest Generation Sub Heap

                      Matured    Matured       Dead
                                               Live

  Matured                                      Dead

      Live            Matured      Live

Tenured Generation Sub Heap

  Matured             Matured    Matured
Java Performance
                                                                          31
                       Java Performance Fundamental | twitter @novathinker
      Garbage Collection Algorithm   artdb@ex-em.com | performeister.tistory.com




• Generational Algorithm


•     Sub Heap      Mark-and-Sweep          Copying       Algorithm


    Fragmentation, Memory             , Copy


•           JVM
Java Performance
                                                                        32
                     Java Performance Fundamental | twitter @novathinker
                                  artdb@ex-em.com | performeister.tistory.com
   Garbage Collection Algorithm




• Train Algorithm
  – Incremental Algorithm

  – Memory
    Mark-and-Copy GC

  – GC                            Suspend
                                      Idea
Java Performance
                                                                                          33
                         Java Performance Fundamental | twitter @novathinker
                                      artdb@ex-em.com | performeister.tistory.com
      Garbage Collection Algorithm



• Train Algorithm

                                     Car :       Memory Block        , Fixed Size


                         RememberSet               RememberSet
                                                                                    Car        root
                          A          B       C     A’      B’   C’           set




Train : Car(         )                               Car
Java Performance
                                                                            34
                         Java Performance Fundamental | twitter @novathinker
                                      artdb@ex-em.com | performeister.tistory.com
       Garbage Collection Algorithm



                                      Car 1-1           Car 1-2
     Train 1                           {R1,E}

                                  A      B      C   D      E      F


R1                                    Car 2-1
     Train 2                           {R2,B}
R2
                                 G
Java Performance
                                                                               34
                          Java Performance Fundamental | twitter @novathinker
                                       artdb@ex-em.com | performeister.tistory.com
        Garbage Collection Algorithm



                                       Free Car       Car 1-1            Car 1-2
     Train 1                                             {C}

                                                  D      E      F    C


R1                                     Car 2-1
     Train 2                            {R2,B}
R2
                                  G


                                       Car 3-1
     Train 3                            {R1}

                                   A      B
Java Performance
                                                                             34
                          Java Performance Fundamental | twitter @novathinker
                                       artdb@ex-em.com | performeister.tistory.com
        Garbage Collection Algorithm



                                       Free Car    Free Car           Free Car
 Free Train




R1                                     Car 2-1
     Train 2                            {R2,B}
R2
                                  G


                                       Car 3-1
     Train 3                            {R1}

                                   A      B
Java Performance
                                                                         35
                      Java Performance Fundamental | twitter @novathinker
                                   artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm




• Train Algorithm


• Suspend




•        Suspend
• Fragmentation
Java Performance
                                                                       36
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Garbage Collection Algorithm




• Adaptive Algorithm
  – Algorithm                          Trade Off
                                     Algorithm
  – Adaptive Algorithm                            Algorithm

  – Heap
    Algorithm
  – Hotspot Ergonomic                           , IBM      Tilt

  –                               Application
Java Performance
         artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




Hotspot JVM
Java Performance
                                                                                      38

                             artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




Hotspot JVM     Garbage Collection
  1) Garbage Collection of Hotspot JVM
  2) Heap of Hotspot JVM
  3) Garbage Collector

  4) Serial Collector
  5) Incremental Collector
  6) Parallel Collector
  7) Parallel Compacting Collector
  8) CMS Collector
  9) Garbage First Collector
Java Performance
                                                                   39
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collection of Hotspot JVM
  – Weak Generational Hypothesis
        • High Infant Mortality
        • Few References from Older to Younger Objects
          Exists
  – Young Generation                  GC Algorithm
        • Speed              – Fast Allocation & TLAB
  – Old Generation GC Algorithm
        •                   – Card Table & Write Barrier
Java Performance
                                                                          40
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
   Hotspot JVM




• Fast Allocation & TLAB
                         T    T    T     T    T     Synchronization
                         1    2    3     4    5          Wait
                 Allocation




                          T          T          T          T          T
                          1          2          3          4          5
TLAB             Allocation Allocation Allocation Allocation Allocation
Java Performance
                                                                       41
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM




• Fast Allocation & TLAB
  – Memory
  – Bump-the-Pointer
  – Multi Thread
  – Hotspot JVM        TLAB
    (Thread Local Allocation Buffer)
       • Thread
       • TLAB
       • Allocation code
                –    10      native instructions
Java Performance
                                                                    42
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM




• Card Table               Write Barrier
  – 1 Byte Card / 512 Byte of Old Generation
  – Old to Young Reference
  – Write Barrier
       • Bytecode Interpreter
       • 2 native instructions
                                           Young Generation



                                                   Old Generation


                                                    Card Table
Java Performance
                                                                     43
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collection of Hotspot JVM
  – Garbage Collection
        • Minor Collection : Young Generation
        • Major Collection : Old Generation
                – Full Collection
                – Method Area GC                 Major    Full
Java Performance
                                                                           44
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Heap of Hotspot JVM



                       Survivor 1

                                    Survivor 2
        Eden                                        Tenured        Permanent




      Young Generation                           Old Generation   Method Area

                           GC
Java Performance
                                                                     45
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
    Hotspot JVM



•    JVM Option
    – Standard Option
          :        JVM             Option
          :-
    – Non-Standard Option
          • JVM
          •          ,                 Parameter
          • -X Option : Macro
          • -XX Option : Micro
Java Performance
                                                                   46
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Automatic Selection
  – Java5                          Garbage Collector,
    Heap Size
  – Hardware Resource                   OS
  – Sever Class             Client Class
Java Performance
                                                                     47
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Automatic Selection
  – Server Class
        •          :2             CPU, 2GB             Physical Mem.
        •         32bit Windows
        • Default Garbage Collector              Parallel Collector
        • Initial Heap Size
                – 1GB       1/64 * Physical Memory
                – 1GB        32MB
        • Max Heap Size : 1GB                   ¼ * Physical Mem.
        • Server Runtime Compiler
Java Performance
                                                                   48
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Automatic Selection
  – Client Class
        • Server Class
        •   Default Garbage Collector          Serial Collector
        •   Initial Heap Size : 4MB
        •   Max Heap Size : 64MB
        •   Client Runtime Compiler
Java Performance
                                                                   49
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Automatic Selection
  –             Option
        • -server : Server Class
        • -client : Client Class
Java Performance
                                                                    50
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
   Hotspot JVM




• Garbage Collector
  – Serial Collector
      • Hotspot JVM          Default Garbage Collector
      • Default Collector
      • Young Generation : Generational Algorithm
      • Old Generation : Mark-and-Compacting
        Algorithm
Java Performance
                                                                    51
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
   Hotspot JVM




• Garbage Collector
  – Parallel Collector
      •           (Throughput)              Garbage Collector
      •   Throughput Garbage Collector
      •   Young Generation
      •   Young Generation : Parallel Copy Algorithm
      •   Old Generation : Mark-and-Compacting
          Algorithm
Java Performance
                                                                   52
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collector
  – Parallel Compacting Collector
        •          Parallel Collector          Old Generation

        • Java SE 5.0 update 6
        • Young Generation : Parallel Copy Algorithm
        • Old Generation : Parallel Compacting
          Algorithm
Java Performance
                                                                   53
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collector
  – CMS Collector
        • CMS : Concurrent Mark-Sweep
        •
        • Old Generation             Pause Time
                       (Low Pause Garbage Collector)
        • Young Generation : Parallel Copy Algorithm
        • Old Generation : Concurrent Mark-and-
          Sweep
                        Algorithm
Java Performance
                                                                   54
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collector
  – Incremental Collector
        • Low Pause Garbage Collector
        • Train Collector
        • Young Generation : Generational Algorithm
        • Old Generation : Train Algorithm
Java Performance
                                                                   55
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Garbage Collector
  – Garbage First Collector
        • Java SE 6 Update 14
        • Train Algorithm
        • Generation
        • Heap Region
            Young , Old Area
        • Realtime                Low Pause
Java Performance
                                                                   56
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Serial Collector
  – Young, Old Generation Serial
    (Single CPU Use)
  – Suspend during Collecting
  – Client Class     Collector
  –
Java Performance
                                                                       57
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



 • Serial Collector
      – Young Generation Collection :
        Generational

Young          Eden

                                           A


                Survivor1 - From               Survivor2 - To

                        B       C                        Empty


Old
Java Performance
                                                                       57
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



 • Serial Collector
      – Young Generation Collection :
        Generational
                                      Minor GC
Young          Eden

                                           A


                Survivor1 - From               Survivor2 - To

                        B       C                   A’          B’    C’


Old
Java Performance
                                                                       57
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



 • Serial Collector
      – Young Generation Collection :
        Generational

Young          Eden

                                        Empty


                Survivor1 - From                Survivor2 - To

                          Empty                      A’          B’   C’


Old
Java Performance
                                                                       58
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



 • Serial Collector


Young          Eden

                                                     A


                Survivor1 - To                Survivor2 - From

                          Empty                                B      M


Old
Java Performance
                                                                        58
                     Java Performance Fundamental | twitter @novathinker
                                  artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



 • Serial Collector

                                       Minor GC
Young          Eden

                                         Empty


                Survivor1 - To                   Survivor2 - From

                    A’    B’                              Empty


Old
                               M’
Java Performance
                                                                       59
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



• Serial Collector
      – Old Generation Collection
                        : Mark-and-Compacting



Old
Java Performance
                                                                       59
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



• Serial Collector
      – Old Generation Collection
                        : Mark-and-Compacting

                                       Mark

Old
                       √                   √            √
Java Performance
                                                                       59
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



• Serial Collector
      – Old Generation Collection
                        : Mark-and-Compacting

                                       Sweep

Old
                       √                   √            √
Java Performance
                                                                       59
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
      Hotspot JVM



• Serial Collector
      – Old Generation Collection
                        : Mark-and-Compacting

                                 Sliding Compaction

Old
              √     √        √




                        Sliding Compaction : Fragmentation
Java Performance
                                                                   60
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Serial Collector
  – JVM Option
        • -XX:+UseSerialGC
           : J2SE5.0, update 6                  Serial Collector

        • -XX:MaxTenuringThreshold=<value>
           : Aging            , default 31
        • -XX:PretenureSizeThreshold=<value>
           : Old Generation directly Allocation
        • -XX:+PrintTenuringDistribution
           : Promotion
Java Performance
                                                                   61
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  –                  Memory, Multi-CPU

  – GC          Multi Thread
  – Large Young Generation
  – Server Class Default Garbage Collector
    (CPU 1                      )
  – Old Generation Collection
     : Serial Collector
Java Performance
                                                                   62
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Young Generation Collection : Parallel
    Copy

         Serial Collector                 Parallel Collector




                            Stop & Copy
Java Performance
                                                                   62
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Young Generation Collection : Parallel
    Copy

         Serial Collector                 Parallel Collector




                            Stop & Copy
Java Performance
                                                                   62
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Young Generation Collection : Parallel
    Copy

         Serial Collector                 Parallel Collector




                            Stop & Copy
Java Performance
                                                                     63
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Promotion Buffer
        • Parallel Local Allocation Buffer(PLAB)
        • Thread
        • Promotion                Thread               Promotion
          Buffer
        • Fragmentation
                – GC Thread
                – Old Generation Size
Java Performance
                                                                     64
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Promotion Buffer




                T1   T2                          T1          T2


                                                  Promotion Buffer
Java Performance
                                                                   65
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Option
        • -XX:+UseParallelGC : Parallel Collector
          (CMS Collector                )
        • -XX:ParallelGCThreads=<value>
           : GC Thread
             default CPU
Java Performance
                                                                   66
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
  – Ergonomics Option
        • -XX:MaxGCPauseMills=<value>
          (1.5+)
           : Maximum Pause Time Goal
            Pause Time
        • -XX:GCTimeRatio=<value>
          (1.5+)
          : Throughput Goal      , default 1%
                            GC Time
        • -XX:+UseAdaptiveSizePolicy
          (1.4.1+)
          :                  Young/Old Sizing
Java Performance
                                                                   67
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
        • -XX:YoungGenerationSizeIncrement=<value>
           : Young Growing Percent, Default = 20
          (1.5+)
        •-
          XX:TenuredGenerationSizeIncrement=<value>
           : Old Growing Percent, Default = 20 (1.5+)
        •-
          XX:AdaptiveSizeDecrementScaleFactor=<value
          >
           : Growing     Shrink  , Default = 4 (1.5+)
        • -XX:+AggressiveHeap
           : Physical Memory Max   Heap
             Hardware Resource
Java Performance
                                                                    68
                 Java Performance Fundamental | twitter @novathinker
                              artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Collector
        • -XX:GCHeapFreeLimit=<value>
          (1.4.1+)
           : GC                 Free Space
                     Heap          (default 5)
             -XX:+UseParallelGC              GC
             OOME
        • -XX:GCTimeLimit=<value>
          (1.4.1+)
           : GC
                                                     (default 90)
                -XX:+UseParallelGC                         GC
                 OOME
Java Performance
                                                                   69
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Parallel Collector    Old Generation
           Collection Algorithm
  –             Parallel Collector
  – Multi CPU
  – Old Generation                Collection
  – Young Generation                  Collection
        • Parallel Collector
        • Parallel Copy Algorithm
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                     70
                  Java Performance Fundamental | twitter @novathinker
                               artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Old Generation Collection
                 : Parallel Compaction

                Parallel Compacting Collector




                                            Mark Phase

                                            Summary Phase

                                            Compaction Phase
Java Performance
                                                                   71
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Mark Phase
        • Generation Region
        • Live Object Marking
        • Parallel Work
Java Performance
                                                                       71
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Mark Phase
        • Generation Region
        • Live Object Marking
        • Parallel Work




                                               
Java Performance
                                                                       72
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Summary Phase
        •   Region              Operation
        •   Single Work
        •   Density
        •   Dense Prefix
        • Dense Prefix                   GC



                                               
Java Performance
                                                                       72
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Summary Phase
        •   Region              Operation
        •   Single Work
        •   Density
        •   Dense Prefix
        • Dense Prefix                     GC



                                               



                            Dense Prefix
Java Performance
                                                                       72
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Summary Phase
        •   Region              Operation
        •   Single Work
        •   Density
        •   Dense Prefix
        • Dense Prefix                     GC



                                               



                            Dense Prefix
Java Performance
                                                                       73
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Compaction Phase
        • Thread                    Region      Collecting
        • Source, Destination
        • Live Object Destination
          Compaction




                                               
Java Performance
                                                                       73
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Compaction Phase
        • Thread                    Region       Collecting
        • Source, Destination
        • Live Object Destination
          Compaction


                                   Destination    Source

                                               
Java Performance
                                                                       73
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Compaction Phase
        • Thread                    Region       Collecting
        • Source, Destination
        • Live Object Destination
          Compaction


                                   Destination    Source

                                               



                                       T1                        T2
Java Performance
                                                                       73
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Compaction Phase
        • Thread                    Region       Collecting
        • Source, Destination
        • Live Object Destination
          Compaction


                                   Destination    Source

                                               



                                       T1
Java Performance
                                                                       73
                    Java Performance Fundamental | twitter @novathinker
                                 artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Compaction Phase
        • Thread                    Region      Collecting
        • Source, Destination
        • Live Object Destination
          Compaction




                                 
Java Performance
                                                                   74
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• Parallel Compacting Collector
  – Option
        • -XX:+UseParallelOldGC                            (+Java6)
           : Parallel Compaction Collector
        • -XX:+UseParallelOldGCCompacting
          (+Java6)
           : Parallel Compaction         , Default True
        • -XX:+UseParallelDensePrefixUpdate
          (+Java6)
           : Dense Prefix          , Default True
Java Performance
                                                                   75
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• CMS Collector
  – Fast Elapsed Time
  –                                                   Old
      Generation
  – Low Latency Collector
  –
  –                                             GC Pause Time

  – Young Generation                  Collection
        • Parallel Collector
Java Performance
                                                                   76
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• CMS Collector
  – Old Generation Collection
                   : Concurrent Mark-Sweep
 Concurrent Mark-Sweep Collector
Java Performance
                                                                   76
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• CMS Collector
  – Old Generation Collection
                   : Concurrent Mark-Sweep
 Concurrent Mark-Sweep Collector




                                          Initial Mark Phase
Java Performance
                                                                   76
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• CMS Collector
  – Old Generation Collection
                   : Concurrent Mark-Sweep
 Concurrent Mark-Sweep Collector




                                          Initial Mark Phase

                                          Concurrent Mark Phase
Java Performance
                                                                   76
                Java Performance Fundamental | twitter @novathinker
                             artdb@ex-em.com | performeister.tistory.com
  Hotspot JVM



• CMS Collector
  – Old Generation Collection
                   : Concurrent Mark-Sweep
 Concurrent Mark-Sweep Collector




                                          Initial Mark Phase

                                          Concurrent Mark Phase

                                          Remark Phase
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection
3장. Garbage Collection

More Related Content

What's hot

Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 

What's hot (20)

IBM JVM 소개 - Oracle JVM 과 비교
IBM JVM 소개 - Oracle JVM 과 비교IBM JVM 소개 - Oracle JVM 과 비교
IBM JVM 소개 - Oracle JVM 과 비교
 
Java Virtual Machine, Call stack, Java Byte Code
Java Virtual Machine, Call stack, Java Byte CodeJava Virtual Machine, Call stack, Java Byte Code
Java Virtual Machine, Call stack, Java Byte Code
 
Java EE vs Spring Framework
Java  EE vs Spring Framework Java  EE vs Spring Framework
Java EE vs Spring Framework
 
Mockito a simple, intuitive mocking framework
Mockito   a simple, intuitive mocking frameworkMockito   a simple, intuitive mocking framework
Mockito a simple, intuitive mocking framework
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Java Generics: a deep dive
Java Generics: a deep diveJava Generics: a deep dive
Java Generics: a deep dive
 
Swing
SwingSwing
Swing
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Java GC
Java GCJava GC
Java GC
 
Tutorial SIAM Data Mining 2012
Tutorial SIAM Data Mining 2012Tutorial SIAM Data Mining 2012
Tutorial SIAM Data Mining 2012
 
CH02:從JDK到IDE
CH02:從JDK到IDECH02:從JDK到IDE
CH02:從JDK到IDE
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 
What is Serialization in Java? | Java Tutorial | Edureka
What is Serialization in Java? | Java Tutorial | EdurekaWhat is Serialization in Java? | Java Tutorial | Edureka
What is Serialization in Java? | Java Tutorial | Edureka
 
Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 
Hibernate
HibernateHibernate
Hibernate
 
RAFT: Recurrent All-Pairs Field Transforms for Optical Flow
RAFT: Recurrent All-Pairs Field Transforms for Optical FlowRAFT: Recurrent All-Pairs Field Transforms for Optical Flow
RAFT: Recurrent All-Pairs Field Transforms for Optical Flow
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 

More from 김 한도

More from 김 한도 (7)

Big Data Myth 1. 우리 회사엔 빅데이터가 없어요
Big Data Myth 1. 우리 회사엔 빅데이터가 없어요Big Data Myth 1. 우리 회사엔 빅데이터가 없어요
Big Data Myth 1. 우리 회사엔 빅데이터가 없어요
 
실시간 빅데이터와 머신 데이터
실시간 빅데이터와 머신 데이터실시간 빅데이터와 머신 데이터
실시간 빅데이터와 머신 데이터
 
In memory as a data innovation
In memory as a data innovationIn memory as a data innovation
In memory as a data innovation
 
Facebook과 연동하기
Facebook과 연동하기Facebook과 연동하기
Facebook과 연동하기
 
7장 Oracle As Datasource
7장 Oracle As Datasource7장 Oracle As Datasource
7장 Oracle As Datasource
 
6장 Thread Synchronization
6장 Thread Synchronization6장 Thread Synchronization
6장 Thread Synchronization
 
5장. Execution Engine
5장. Execution Engine5장. Execution Engine
5장. Execution Engine
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

3장. Garbage Collection

  • 1. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Garbage Collection
  • 2. Java Performance 2 artdb@ex-em.com | performeister.tistory.com | twitter @novathinker 1) Garbage Collection 2) Garbage Collection Algorithm 3) Hotspot JVM Garbage Collection 4) IBM JVM Garbage Collection
  • 3. Java Performance 3 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection • Garbage Collection – Garbage Collector – Unreferenced Memory – – Fragmentation “Heap storage for objects is reclaimed by an automatic storage management system (typically a garbage collector); objects are never explicitly deallocated.” - Java Virtual Machine Speculation, Section 3.5.3 [JVMS2 1999]
  • 4. Java Performance 4 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ! Garbage Collector
  • 5. Java Performance 4 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ! object Garbage Collector Heap memory .
  • 6. Java Performance 4 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ! object Garbage Collector Heap memory . Garbage Collection Memory Recycling Heap Fragmentation .
  • 7. Java Performance 4 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ! object Garbage Collector Heap memory . Garbage Collection Memory Recycling Heap Fragmentation . • JVM heap new, newarray, anewarray, multianewarray instruction instruction • Garbage Collection • JVM Spec Garbage Collection Algorithm
  • 8. Java Performance 5 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ! Garbage Collector
  • 9. Java Performance 5 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Memory ! Memory System Garbage Collector crash .
  • 10. Java Performance 5 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Memory ! Memory System Garbage Collector crash . Program CPU Time Scheduling .
  • 11. Java Performance 6 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection • Garbage Collection – Garbage Collection
  • 12. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection
  • 13. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ROOT SET
  • 14. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ROOT SET Local Variable, Operand Stack
  • 15. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ROOT SET Local Variable, Operand Stack Class Constant Pool Reference
  • 16. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection ROOT SET Local Variable, Unreleased Native Operand Stack Method Object Reference Class Constant Pool Reference
  • 17. Java Performance 8 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Colletion • Root Set
  • 18. Java Performance 9 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Colletion • Memory Leak class Main{ public static void main (String args[]) { Leak lk = new Leak(); for(int a=0; a<9000000; a++) { lk.addList(a); lk.removeStr(a); } } } class Leak { ArrayList lst = new ArrayList(); public void addList(int a) { lst.add(" "+a); } public void removeStr(int i) { Object obj = lst.get(i); obj = null; } }
  • 19. Java Performance 9 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Colletion • Memory Leak class Main{ public static void main (String args[]) { Leak lk = new Leak(); for(int a=0; a<9000000; a++) { lk.addList(a); lk.removeStr(a); } } Why leak? } class Leak { ArrayList lst = new ArrayList(); public void addList(int a) { lst.add(" "+a); } public void removeStr(int i) { Object obj = lst.get(i); obj = null; } }
  • 20. Java Performance 10 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm
  • 21. Java Performance 11 artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Garbage Collection Algorithm 1) Reference Counting Algorithm 2) Mark-and-Sweep Algorithm 3) Mark-and-Compacting Algorithm 4) Copying Algorithm 5) Generational Algorithm 6) Train Algorithm 7) Adaptive Algorithm
  • 22. Java Performance 12 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Garbage Collection Algorithm
  • 23. Java Performance 12 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Garbage Collection Algorithm Garbage Object Detection • Live Object : Root Set • Garbage Object
  • 24. Java Performance 12 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Garbage Collection Algorithm Garbage Object Detection • Live Object : Root Set • Garbage Object Garbage Object • Heap Memory Reclaim
  • 25. Java Performance 13 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Reference Counting Algorithm • Mark-and-Sweep Algorithm • Copying Algorithm • Mark-and-Compacting Algorithm • Generational Algorithm • Train Algorithm • Adaptive Algorithm
  • 26. Java Performance 14 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Reference Counting Algorithm – GC – Object Reference count – Reference Count 0 GC – Object GC Object Object Object Count Count
  • 27. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer RefCnt 00 Integer RefCnt 00 Integer RefCnt 00
  • 28. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); RefCnt 00 Integer RefCnt 00 Integer RefCnt 00
  • 29. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 RefCnt 01 0 Integer RefCnt 00 Integer RefCnt 00
  • 30. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; RefCnt 01 0 Integer RefCnt 00 Integer RefCnt 00
  • 31. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer RefCnt 00 Integer RefCnt 00
  • 32. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer Object a = new Ingeter(1); RefCnt 00 Integer RefCnt 00
  • 33. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer a 1 Object a = new Ingeter(1); 0 RefCnt 01 Integer RefCnt 00
  • 34. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer a 1 Object a = new Ingeter(1); 0 RefCnt 01 Object b = new Ingeter(2); Integer RefCnt 00
  • 35. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer a 1 Object a = new Ingeter(1); 0 RefCnt 01 Object b = new Ingeter(2); Integer 2 b RefCnt 01 0
  • 36. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer a 1 Object a = new Ingeter(1); 0 RefCnt 01 Object b = new Ingeter(2); Integer a = b; 2 b RefCnt 01 0
  • 37. Java Performance 15 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Integer Object a = new Ingeter(1); a 1 Object b = a; b RefCnt 01 0 2 Integer a 1 Object a = new Ingeter(1); 0 RefCnt 01 Object b = new Ingeter(2); Integer a = b; 2 b RefCnt 01 0 2
  • 38. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm
  • 39. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2));
  • 40. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2)); Integer 1 TwoInteger RefCnt 00 1 ref a RefCnt 00 1 Integer 2 RefCnt 01 0
  • 41. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2)); a = null; Integer 1 TwoInteger RefCnt 00 1 ref a RefCnt 00 1 Integer 2 RefCnt 01 0
  • 42. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2)); a = null; Integer 1 TwoInteger RefCnt 00 1 ref a RefCnt 00 1 Integer 2 RefCnt 01 0
  • 43. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2)); a = null; Integer 1 TwoInteger RefCnt 00 1 ref a GC RefCnt 00 1 Integer 2 RefCnt 01 0
  • 44. Java Performance 16 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Object a = new TwoIngeter(new Integer(1), new Integer(2)); a = null; Integer 1 RefCnt 00 1 a Integer 2 RefCnt 01 0 0
  • 45. Java Performance 17 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Reference Counting Algorithm • Garbage Object • Garbage • Garbage Collector ( ) • Reference Count • Linked List Reference Count 0 Leak
  • 46. Java Performance 18 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm a Linked List next ref RefCnt 02 Linked List next ref RefCnt 11 Linked List next ref RefCnt 01
  • 47. Java Performance 18 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm a Linked List next ref 2 RefCnt 01 Linked List next ref RefCnt 11 Linked List next ref RefCnt 01
  • 48. Java Performance 19 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Mark-and-Sweep Algorithm – Root Set Reference – Tracing Algorithm – Mark Sweep • Mark : Live Object Mark (Object flag or bitmap table ) • Sweep : Heap Mark
  • 49. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm R ref ref ref  flag  flag  flag ref ref ref  flag  flag  flag ref ref ref  flag  flag  flag
  • 50. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Mark Phase R ref ref ref  flag  flag  flag ref ref ref  flag  flag  flag ref ref ref  flag  flag  flag
  • 51. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Mark Phase R ref ref ref   flag   flag   flag ref ref ref  flag  flag   flag ref ref ref  flag   flag   flag
  • 52. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Sweep Phase R ref ref ref   flag   flag   flag ref ref ref  flag  flag   flag ref ref ref  flag   flag   flag
  • 53. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Sweep Phase R ref ref ref   flag   flag   flag ref   flag ref ref   flag   flag
  • 54. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm R ref ref ref   flag   flag   flag ref   flag ref ref   flag   flag
  • 55. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm R ref ref ref  flag  flag  flag ref  flag ref ref  flag  flag
  • 56. Java Performance 21 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Mark-and-Sweep Algorithm • Reference • Reference Overhead • Suspend • Fragmentation  Memory
  • 57. Java Performance 22 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Mark-and-Compacting Algorithm – Fragmentation Memory Algorithm – Mark Compaction • Mark Phase : Live Object Mark • Compaction : Live Object Memory – Handle • Mark Phase : Handle Marking • Compaction : Object Handle Update
  • 58. Java Performance 23 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Mark-and-Compacting Algorithm – Compaction Arbitrary Worst 4 1 2 3 1 2 3 4 Linear 1 3 4 2 • Arbitrary : Sliding • Linear : Pointer 1 2 3 4 • Sliding : Allocation Best
  • 59. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Handle 0  flag 1  flag T22 A 5 Z 1 2  flag T11 3  flag T12 B 9 W 7 4  flag 5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag 9  flag T32
  • 60. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Mark Phase Handle 0  flag 1  flag T22 A 5 Z 1 2  flag T11 3  flag T12 B 9 W 7 4  flag 5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag 9  flag T32
  • 61. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Mark Phase Handle 0  flag 1  flag T22 A 5 Z 1  2  flag T11 3  flag T12 B 9 W 7 4  flag  5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag  9  flag T32
  • 62. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Mark Phase Handle 0  flag 1  flag T22 A 5 Z 1  2  flag T11 3  flag T12 B 9 W 7 4  flag  5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag  9  flag T32
  • 63. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Mark Phase Handle 0  flag 1  flag T22 A 5 Z 1  2  flag T11 3  flag T12 B 9 W 7 4  flag  5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag  9  flag T32
  • 64. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Compation Phase Handle 0  flag 1  flag T22 A 5 Z 1  2  flag T11 3  flag T12 B 9 W 7 4  flag  5  flag T21 6  flag Q 3 C 2 7  flag T31 8  flag  9  flag T32
  • 65. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Compation Phase Handle 0  flag 1  flag T22 A 5  2  flag T11 3  flag T12 B 9 4  flag  5  flag T21 6  flag C 2 7  flag T31 8  flag  9  flag T32
  • 66. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Compation Phase Handle 0  flag 1  flag T22 A 5 B 9  2  flag T11 3  flag T12 4  flag  5  flag T12 T21 6  flag C 2 7  flag T31 8  flag  9  flag T32
  • 67. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Compation Phase Handle 0  flag 1  flag T22 A 5 B 9  2  flag T11 3  flag T12 C 2 4  flag  5  flag T12 T21 6  flag 7  flag T31 8  flag  9  flag T21 T32
  • 68. Java Performance 24 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm 2 Handle 0  flag 1  flag T22 A 5 B 9 2  flag T11 3  flag T12 C 2 4  flag 5  flag T12 T21 6  flag 7  flag T31 8  flag 9  flag T21 T32
  • 69. Java Performance 25 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Mark-and-Compacting Algorithm • Fragmentation • Memory • Reference Object Access Overhead • Suspend
  • 70. Java Performance 26 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Copying Algorithm – Stop-the-Copy Algorithm – Heap Active Inactive – Active Object – Active Live Object Inactive Copy
  • 71. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Active Area A C B R Inactive Area
  • 72. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Active Area A C null null B null R Inactive Area
  • 73. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Active Area A C null B null R A’ null Inactive Area
  • 74. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Active Area A C null B R A’ B’ null null Inactive Area
  • 75. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Active Area A C B R A’ B’ C’ null null null Inactive Area
  • 76. Java Performance 27 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Inactive Area R A’ B’ C’ null null null Active Area
  • 77. Java Performance 28 Java Performance Fundamental | twitter @novathinker Garbage Collection Algorithm artdb@ex-em.com | performeister.tistory.com • Copying Algorithm • Fragmentation  Inactive Copy • GC Suspend • Copy Overhead •
  • 78. Java Performance 29 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Generational Algorithm – Copying Algorithm • Copy • Object • Object • Long Lived Object – Heap age sub heap Youngest Generation Sub heap GC – Object Age
  • 79. Java Performance 30 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Youngest Generation Sub Heap Matured Live Live Dead Dead Live Matured Dead Live Dead Dead Live Dead Dead Matured Tenured Generation Sub Heap
  • 80. Java Performance 30 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Youngest Generation Sub Heap Live Live Dead Dead Live Dead Live Dead Dead Live Dead Dead Tenured Generation Sub Heap GC Matured Matured Matured
  • 81. Java Performance 30 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Youngest Generation Sub Heap Live Live Live Live Live Tenured Generation Sub Heap GC Matured Matured Matured
  • 82. Java Performance 30 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Youngest Generation Sub Heap Matured Matured Dead Live Matured Dead Live Matured Live Tenured Generation Sub Heap Matured Matured Matured
  • 83. Java Performance 31 Java Performance Fundamental | twitter @novathinker Garbage Collection Algorithm artdb@ex-em.com | performeister.tistory.com • Generational Algorithm • Sub Heap Mark-and-Sweep Copying Algorithm Fragmentation, Memory , Copy • JVM
  • 84. Java Performance 32 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Train Algorithm – Incremental Algorithm – Memory Mark-and-Copy GC – GC Suspend Idea
  • 85. Java Performance 33 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Train Algorithm Car : Memory Block , Fixed Size RememberSet RememberSet Car root A B C A’ B’ C’ set Train : Car( ) Car
  • 86. Java Performance 34 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Car 1-1 Car 1-2 Train 1 {R1,E} A B C D E F R1 Car 2-1 Train 2 {R2,B} R2 G
  • 87. Java Performance 34 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Free Car Car 1-1 Car 1-2 Train 1 {C} D E F C R1 Car 2-1 Train 2 {R2,B} R2 G Car 3-1 Train 3 {R1} A B
  • 88. Java Performance 34 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm Free Car Free Car Free Car Free Train R1 Car 2-1 Train 2 {R2,B} R2 G Car 3-1 Train 3 {R1} A B
  • 89. Java Performance 35 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Train Algorithm • Suspend • Suspend • Fragmentation
  • 90. Java Performance 36 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Garbage Collection Algorithm • Adaptive Algorithm – Algorithm Trade Off  Algorithm – Adaptive Algorithm Algorithm – Heap Algorithm – Hotspot Ergonomic , IBM Tilt – Application
  • 91. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Hotspot JVM
  • 92. Java Performance 38 artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Hotspot JVM Garbage Collection 1) Garbage Collection of Hotspot JVM 2) Heap of Hotspot JVM 3) Garbage Collector 4) Serial Collector 5) Incremental Collector 6) Parallel Collector 7) Parallel Compacting Collector 8) CMS Collector 9) Garbage First Collector
  • 93. Java Performance 39 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collection of Hotspot JVM – Weak Generational Hypothesis • High Infant Mortality • Few References from Older to Younger Objects Exists – Young Generation GC Algorithm • Speed – Fast Allocation & TLAB – Old Generation GC Algorithm • – Card Table & Write Barrier
  • 94. Java Performance 40 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Fast Allocation & TLAB T T T T T Synchronization 1 2 3 4 5 Wait Allocation T T T T T 1 2 3 4 5 TLAB Allocation Allocation Allocation Allocation Allocation
  • 95. Java Performance 41 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Fast Allocation & TLAB – Memory – Bump-the-Pointer – Multi Thread – Hotspot JVM TLAB (Thread Local Allocation Buffer) • Thread • TLAB • Allocation code – 10 native instructions
  • 96. Java Performance 42 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Card Table Write Barrier – 1 Byte Card / 512 Byte of Old Generation – Old to Young Reference – Write Barrier • Bytecode Interpreter • 2 native instructions Young Generation Old Generation Card Table
  • 97. Java Performance 43 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collection of Hotspot JVM – Garbage Collection • Minor Collection : Young Generation • Major Collection : Old Generation – Full Collection – Method Area GC Major Full
  • 98. Java Performance 44 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Heap of Hotspot JVM Survivor 1 Survivor 2 Eden Tenured Permanent Young Generation Old Generation Method Area GC
  • 99. Java Performance 45 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • JVM Option – Standard Option : JVM Option :- – Non-Standard Option • JVM • , Parameter • -X Option : Macro • -XX Option : Micro
  • 100. Java Performance 46 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Automatic Selection – Java5 Garbage Collector, Heap Size – Hardware Resource OS – Sever Class Client Class
  • 101. Java Performance 47 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Automatic Selection – Server Class • :2 CPU, 2GB Physical Mem. • 32bit Windows • Default Garbage Collector Parallel Collector • Initial Heap Size – 1GB 1/64 * Physical Memory – 1GB 32MB • Max Heap Size : 1GB ¼ * Physical Mem. • Server Runtime Compiler
  • 102. Java Performance 48 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Automatic Selection – Client Class • Server Class • Default Garbage Collector Serial Collector • Initial Heap Size : 4MB • Max Heap Size : 64MB • Client Runtime Compiler
  • 103. Java Performance 49 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Automatic Selection – Option • -server : Server Class • -client : Client Class
  • 104. Java Performance 50 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – Serial Collector • Hotspot JVM Default Garbage Collector • Default Collector • Young Generation : Generational Algorithm • Old Generation : Mark-and-Compacting Algorithm
  • 105. Java Performance 51 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – Parallel Collector • (Throughput) Garbage Collector • Throughput Garbage Collector • Young Generation • Young Generation : Parallel Copy Algorithm • Old Generation : Mark-and-Compacting Algorithm
  • 106. Java Performance 52 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – Parallel Compacting Collector • Parallel Collector Old Generation • Java SE 5.0 update 6 • Young Generation : Parallel Copy Algorithm • Old Generation : Parallel Compacting Algorithm
  • 107. Java Performance 53 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – CMS Collector • CMS : Concurrent Mark-Sweep • • Old Generation Pause Time (Low Pause Garbage Collector) • Young Generation : Parallel Copy Algorithm • Old Generation : Concurrent Mark-and- Sweep Algorithm
  • 108. Java Performance 54 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – Incremental Collector • Low Pause Garbage Collector • Train Collector • Young Generation : Generational Algorithm • Old Generation : Train Algorithm
  • 109. Java Performance 55 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Garbage Collector – Garbage First Collector • Java SE 6 Update 14 • Train Algorithm • Generation • Heap Region Young , Old Area • Realtime Low Pause
  • 110. Java Performance 56 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Young, Old Generation Serial (Single CPU Use) – Suspend during Collecting – Client Class Collector –
  • 111. Java Performance 57 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Young Generation Collection : Generational Young Eden A Survivor1 - From Survivor2 - To B C Empty Old
  • 112. Java Performance 57 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Young Generation Collection : Generational Minor GC Young Eden A Survivor1 - From Survivor2 - To B C A’ B’ C’ Old
  • 113. Java Performance 57 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Young Generation Collection : Generational Young Eden Empty Survivor1 - From Survivor2 - To Empty A’ B’ C’ Old
  • 114. Java Performance 58 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector Young Eden A Survivor1 - To Survivor2 - From Empty B M Old
  • 115. Java Performance 58 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector Minor GC Young Eden Empty Survivor1 - To Survivor2 - From A’ B’ Empty Old M’
  • 116. Java Performance 59 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Old Generation Collection : Mark-and-Compacting Old
  • 117. Java Performance 59 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Old Generation Collection : Mark-and-Compacting Mark Old √ √ √
  • 118. Java Performance 59 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Old Generation Collection : Mark-and-Compacting Sweep Old √ √ √
  • 119. Java Performance 59 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – Old Generation Collection : Mark-and-Compacting Sliding Compaction Old √ √ √ Sliding Compaction : Fragmentation
  • 120. Java Performance 60 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Serial Collector – JVM Option • -XX:+UseSerialGC : J2SE5.0, update 6 Serial Collector • -XX:MaxTenuringThreshold=<value> : Aging , default 31 • -XX:PretenureSizeThreshold=<value> : Old Generation directly Allocation • -XX:+PrintTenuringDistribution : Promotion
  • 121. Java Performance 61 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Memory, Multi-CPU – GC Multi Thread – Large Young Generation – Server Class Default Garbage Collector (CPU 1 ) – Old Generation Collection : Serial Collector
  • 122. Java Performance 62 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Young Generation Collection : Parallel Copy Serial Collector Parallel Collector Stop & Copy
  • 123. Java Performance 62 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Young Generation Collection : Parallel Copy Serial Collector Parallel Collector Stop & Copy
  • 124. Java Performance 62 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Young Generation Collection : Parallel Copy Serial Collector Parallel Collector Stop & Copy
  • 125. Java Performance 63 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Promotion Buffer • Parallel Local Allocation Buffer(PLAB) • Thread • Promotion Thread Promotion Buffer • Fragmentation – GC Thread – Old Generation Size
  • 126. Java Performance 64 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Promotion Buffer T1 T2 T1 T2 Promotion Buffer
  • 127. Java Performance 65 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Option • -XX:+UseParallelGC : Parallel Collector (CMS Collector ) • -XX:ParallelGCThreads=<value> : GC Thread default CPU
  • 128. Java Performance 66 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector – Ergonomics Option • -XX:MaxGCPauseMills=<value> (1.5+) : Maximum Pause Time Goal Pause Time • -XX:GCTimeRatio=<value> (1.5+) : Throughput Goal , default 1% GC Time • -XX:+UseAdaptiveSizePolicy (1.4.1+) : Young/Old Sizing
  • 129. Java Performance 67 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector • -XX:YoungGenerationSizeIncrement=<value> : Young Growing Percent, Default = 20 (1.5+) •- XX:TenuredGenerationSizeIncrement=<value> : Old Growing Percent, Default = 20 (1.5+) •- XX:AdaptiveSizeDecrementScaleFactor=<value > : Growing Shrink , Default = 4 (1.5+) • -XX:+AggressiveHeap : Physical Memory Max Heap Hardware Resource
  • 130. Java Performance 68 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Collector • -XX:GCHeapFreeLimit=<value> (1.4.1+) : GC Free Space Heap (default 5) -XX:+UseParallelGC GC OOME • -XX:GCTimeLimit=<value> (1.4.1+) : GC (default 90) -XX:+UseParallelGC GC OOME
  • 131. Java Performance 69 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Parallel Collector Old Generation Collection Algorithm – Parallel Collector – Multi CPU – Old Generation Collection – Young Generation Collection • Parallel Collector • Parallel Copy Algorithm
  • 132. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 133. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 134. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 135. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 136. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 137. Java Performance 70 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Old Generation Collection : Parallel Compaction Parallel Compacting Collector Mark Phase Summary Phase Compaction Phase
  • 138. Java Performance 71 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Mark Phase • Generation Region • Live Object Marking • Parallel Work
  • 139. Java Performance 71 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Mark Phase • Generation Region • Live Object Marking • Parallel Work       
  • 140. Java Performance 72 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Summary Phase • Region Operation • Single Work • Density • Dense Prefix • Dense Prefix GC       
  • 141. Java Performance 72 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Summary Phase • Region Operation • Single Work • Density • Dense Prefix • Dense Prefix GC        Dense Prefix
  • 142. Java Performance 72 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Summary Phase • Region Operation • Single Work • Density • Dense Prefix • Dense Prefix GC        Dense Prefix
  • 143. Java Performance 73 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Compaction Phase • Thread Region Collecting • Source, Destination • Live Object Destination Compaction       
  • 144. Java Performance 73 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Compaction Phase • Thread Region Collecting • Source, Destination • Live Object Destination Compaction Destination Source       
  • 145. Java Performance 73 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Compaction Phase • Thread Region Collecting • Source, Destination • Live Object Destination Compaction Destination Source        T1 T2
  • 146. Java Performance 73 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Compaction Phase • Thread Region Collecting • Source, Destination • Live Object Destination Compaction Destination Source        T1
  • 147. Java Performance 73 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Compaction Phase • Thread Region Collecting • Source, Destination • Live Object Destination Compaction       
  • 148. Java Performance 74 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • Parallel Compacting Collector – Option • -XX:+UseParallelOldGC (+Java6) : Parallel Compaction Collector • -XX:+UseParallelOldGCCompacting (+Java6) : Parallel Compaction , Default True • -XX:+UseParallelDensePrefixUpdate (+Java6) : Dense Prefix , Default True
  • 149. Java Performance 75 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • CMS Collector – Fast Elapsed Time – Old Generation – Low Latency Collector – – GC Pause Time – Young Generation Collection • Parallel Collector
  • 150. Java Performance 76 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • CMS Collector – Old Generation Collection : Concurrent Mark-Sweep Concurrent Mark-Sweep Collector
  • 151. Java Performance 76 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • CMS Collector – Old Generation Collection : Concurrent Mark-Sweep Concurrent Mark-Sweep Collector Initial Mark Phase
  • 152. Java Performance 76 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • CMS Collector – Old Generation Collection : Concurrent Mark-Sweep Concurrent Mark-Sweep Collector Initial Mark Phase Concurrent Mark Phase
  • 153. Java Performance 76 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot JVM • CMS Collector – Old Generation Collection : Concurrent Mark-Sweep Concurrent Mark-Sweep Collector Initial Mark Phase Concurrent Mark Phase Remark Phase