Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
www.luxoft.com
Evolution of Garbage Collection
in Java Virtual Machine
BJUG – June 2015
Ionuț Baloșin
This version contain...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 Ionuț Baloșin
- technical leader at Luxoft.com
- ...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 Java HotSpot VM Memory Layout
 Garbage Collector...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
1. Weak Generation Hypothesis
 Memory Management a...
www.luxoft.com
Heap Space
Eden
Young Generation Old Generation
To
Survivor
From
Survivor Tenured
Method Area
PermGen / Met...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Garbage Collectors
 Frequency of Minor GC is dicta...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Quizz
 How to allow concurrent Eden allocations ag...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 Choices for designing/selecting a GC algorithm:
-...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
1. Train (Incremental) GC
2. Serial GC
3. Parallel ...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Advices of choosing the appropriate Garbage Collect...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 When calling System.gc(), what type of Garbage Co...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Garbage Collection friendly programming
 -XX:+DoEs...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
1. What is the FinalizerSample object retention con...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Garbage Collection friendly programming
 Avoid to ...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
'Mandatory' Flags
 -verbose:gc
 print basic GC in...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 Visual representations of Heap memory pools durin...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
 Run with -Xloggc:<file> / -verbose:gc and check t...
www.luxoft.com
Evolution of Garbage Collection in Java Virtual Machine
Bibliography
www.luxoft.com
THANK YOU
Prochain SlideShare
Chargement dans…5
×

Evolution of Garbage Collection in HotSpot Java Virtual Machine

1 071 vues

Publié le

Evolution and performance of Garbage Collection algorithms for the Oracle HotSpot Virtual Machine, starting from the first few versions of Java up to the latest.

Publié dans : Technologie
  • Soyez le premier à commenter

Evolution of Garbage Collection in HotSpot Java Virtual Machine

  1. 1. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine BJUG – June 2015 Ionuț Baloșin This version contains partial content. For more details please contact me at ionutbalosin@yahoo.com
  2. 2. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  Ionuț Baloșin - technical leader at Luxoft.com - 8 years of Java experience  3 years of Java financial applications - author of “Java Performance And Tuning” – Luxoft-Training.com course About
  3. 3. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  Java HotSpot VM Memory Layout  Garbage Collectors  Advices of choosing the appropriate Garbage Collector  Garbage Collection friendly programming  Garbage Collection Viewer tool  Demos  Quizzes Agenda
  4. 4. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine 1. Weak Generation Hypothesis  Memory Management and Garbage Collector 2. Use runtime information to take best decisions  Execution Engine Copyright - Oracle Corporation Java Hot Spot VM Rationales
  5. 5. www.luxoft.com Heap Space Eden Young Generation Old Generation To Survivor From Survivor Tenured Method Area PermGen / Metaspace Runtime Constant Pool Field & Method Data Code for methods and constructors Major / Full Garbage Collection Evolution of Garbage Collection in Java Virtual Machine Java Hot Spot VM Memory Layout New object allocation Minor Garbage Collection Object promotion
  6. 6. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Garbage Collectors  Frequency of Minor GC is dictated by - application object allocation rate - size of the Eden space  Frequency of object promotion is dictated by - frequency of Minor GCs - size of the Survivor spaces  Ideal situation - promote as little as possible - never experience need for Old generation collection  Minor GCs are the fastest GC
  7. 7. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Quizz  How to allow concurrent Eden allocations against Eden from multiple Threads?  Thread Local Allocation Buffers (TLAB) - used by default in JVM - is pre-allocated for each thread - when a thread needs to allocate memory in TLAB, it uses “bump the pointer” Thread 1 Thread 2 Thread 3 Eden Young Generation To Survivor From Survivor TLAB TLAB TLAB
  8. 8. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  Choices for designing/selecting a GC algorithm: - Serial vs. Parallel - Concurrent vs. Stop-the-World - Compacting vs. Non-Compacting vs. Copying Garbage Collectors
  9. 9. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine 1. Train (Incremental) GC 2. Serial GC 3. Parallel GC 4. Parallel Old GC (Parallel Compacting GC) 5. Concurrent Mark & Sweep GC (or "CMS") 6. Garbage First (G1) GC Garbage Collectors
  10. 10. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Advices of choosing the appropriate Garbage Collector  Start with Parallel Old GC - Parallel Old GC offers the fastest minor GC times due to Promotional Local Allocation Buffers - If you can avoid full GCs, you’ll likely achieve  the best throughput  lowest latency  smallest footprint  Move to CMS or G1 if needed (especially for Tenured generation collections) - CMS minor GC times are slower due to promotion into Free Lists Space - CMS full GC is in essence quicker due to concurrent Tenured collection - G1 full GC offers better results due to “partial” and concurrent Tenured collection and it also avoids fragmentation
  11. 11. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  When calling System.gc(), what type of Garbage Collection is triggered? a. minor Garbage Collection b. full Garbage Collection Quizz System.gc() requests a full Garbage Collection. There is no way to programmatically trigger a minor collection.
  12. 12. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Garbage Collection friendly programming  -XX:+DoEscapeAnalysis - “Java object instances are allocated always on the Heap” – not really  - Escape Analysis allocates Java objects on the stack only if the an object has a local scope and does not escape  Data Structure Re-sizing - avoid re-sizing of array backed collections / StringBuilder / StringBuffer  re-sizing leads to unnecessary object allocation (fragmentation)  Advice: use the constructor with an explicit size for the backing arraydouble sum(int[] values) { double result = 0; for (int i = 1; i < values.length; i++) { DoubleSlot slot = new DoubleSlot(values[i - 1], values[i]); result += slotValue(slot); // slot.value1 + slot.value2 } return result; }
  13. 13. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine 1. What is the FinalizerSample object retention consequences if ClassWithFinalizer has a finalize() method? 2. How to lower the object retention? Quizz public class FinalizerSample { private ClassWithFinalizer classWithFinalizer; /* composite field */ private byte[] buffer = new byte[1024 * 1024 * 2]; } public class FinalizerSample extends ClassWithFinalizer { private byte[] buffer = new byte[1024 * 1024 * 2]; }
  14. 14. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Garbage Collection friendly programming  Avoid to use finalize() - is called only once when an object is about to get garbage collected - requires at least 2 Garbage Collector cycles and these cycles are slower - Advice :  avoid finalize() for the “correct” operation of your application  finalize() should only be used for cleanup of (usually non-Java) resources  Inner Classes - have an implicit reference to the outer instance  can potentially increase object retention - more live objects at GC time , so increased GC duration
  15. 15. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine 'Mandatory' Flags  -verbose:gc  print basic GC info  -XX:+PrintGCDetails  print elaborated GC info  -Xloggc:<file>  redirects GC output to a file instead of console  -XX:+AlwaysTenure  promote all objects surviving Young Collection immediately  -XX:+NeverTenure  objects from young space will never get promoted while Survivor space is large enough to keep them  -Xms<size>  set the initial size of Heap  -Xmx<size>  set the maximum size of Heap  -XX:MaxPermSize=<size>  set the maximum size of Perm Gen  -XX:MaxMetaspaceSize=<size>  set the maximum size of MetaSpace
  16. 16. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  Visual representations of Heap memory pools during a stress test a. -XX:+UseParallelOldGC b. -XX:+UseConcMarkSweepGCG1 c. -XX:+UseG1GC d. -XX:+AlwaysTenure  in all cases run with -Xms1280m -Xmx1280m  VisualVM - Visual GC plugin Demo #1
  17. 17. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine  Run with -Xloggc:<file> / -verbose:gc and check the Garbage Collector output  GCViewer - http://www.tagtraum.com/gcviewer.html  HPJmeter - http://www.hp.com/ Demo #2
  18. 18. www.luxoft.com Evolution of Garbage Collection in Java Virtual Machine Bibliography
  19. 19. www.luxoft.com THANK YOU

×