Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Principios básicos de Garbage Collector en Java

203 views

Published on

Tutoral demostrativo de uso y situaciones comunes de fuga de memoria en el Garbage Collector de Java. ¿Porqué mis aplicaciones explotan?

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Principios básicos de Garbage Collector en Java

  1. 1. Introducci´on al GC en Java - Porqu´e mis aplicaciones explotan V´ıctor Orozco - @tuxtor 10 de mayo de 2018 GuateJUG 1
  2. 2. V´ıctor Orozco • Developer (JVM/Open Source Advocate) • JUG Leader • Consultor independiente • Profesor universitario • @tuxtor • http://vorozco.com • http://tuxtor.shekalug.org 2
  3. 3. TLDR Explosi´on • Mala selecci´on de tipos de dato • Muchas variables y referencias (memory leak) • Algoritmos innecesariamente complejos • Muchas apps y poca memoria 3
  4. 4. Memoria en Java
  5. 5. Stack vs heap Figura 1: Stack y Heap 4
  6. 6. Stack vs heap Figura 2: Java main thread 5
  7. 7. Mark and sweep Figura 3: Mark and sweep, Credits: https://plumbr.io 6
  8. 8. Mark and sweep Figura 4: Mark and sweep - Mark, Credits: https://plumbr.io Generalmente DFS 7
  9. 9. Mark and sweep Figura 5: Mark and sweep - sweep, Credits: https://plumbr.io 8
  10. 10. Mark and sweep Figura 6: Mark - sweep, Credits: https://plumbr.io 9
  11. 11. Mark - sweep - compact Figura 7: Mark - sweep - compact, Credits: https://plumbr.io 10
  12. 12. Mark and copy Figura 8: Mark and copy, Credits: https://plumbr.io 11
  13. 13. Demo 0 - Generaci´on de objetos //... Stream <Integer > nums = Stream.iterate (1, n -> n + 1); nums.forEach(num -> { System.out.println(num); try{Thread.sleep (10);} catch( InterruptedException ex){} } ); //... 12
  14. 14. Generational garbage collectors
  15. 15. Generational GC(HotSpot) Figura 9: Generational GC, Credits: Oracle 13
  16. 16. Generational GC(HotSpot) Figura 10: Generational GC, Credits: Oracle 14
  17. 17. Generational GC(HotSpot) Figura 11: Generational GC, Credits: Oracle 15
  18. 18. Generational GC(HotSpot) Figura 12: Generational GC, Credits: Oracle 16
  19. 19. Generational GC(HotSpot) Figura 13: Generational GC, Credits: Oracle 17
  20. 20. Generational GC(HotSpot) Figura 14: Generational GC, Credits: Oracle 18
  21. 21. Generational GC(HotSpot) Figura 15: Generational GC, Credits: Oracle 19
  22. 22. Demo 1 - GC Generacional //... Stream <Integer > nums = Stream.iterate (1, n -> n + 1); var numeros = new ArrayList <Integer >(); nums.forEach(num -> { System.out.println(num); numeros.add(num); }); //... java -XX:+UseSerialGC DemoMemoriaGenerations 20
  23. 23. GC en HotSpot
  24. 24. Recolectores de basura • Serial GC para Young y Old generations • Parallel GC para Young y Old generations • Parallel New para Young + Concurrent Mark and Sweep (CMS) para Old Generation • G1GC, para Young y Old generations 21
  25. 25. SerialGC • Young: Mark-Copy • Old: Mark-Sweep-Compact • java − XX : +UseSerialGCcom.nabenik.MyExecClass 22
  26. 26. ParallelGC • Young: Mark-Copy • Old: Mark-Sweep-Compact • Stop-the-world en ambas regiones • java − XX : +UseParallelGCcom.nabenik.MyExecClass 23
  27. 27. CMS • Young: Mark-Copy - Stop the world • Old: (Mostly )Concurrent Mark Sweep (Paralelo) • java − XX : +UseConcMarkSweepGCcom.nabenik.MyExecClass 24
  28. 28. Como luchar CONTRA un Garbage Collector
  29. 29. Demo 1 - Referencias + Mal tipo de dato //... var lasReferencias = new ArrayList <String >(); Stream <Integer > numeros = Stream.iterate (1, n -> ++n); numeros.forEach(n -> { lasReferencias.add(n + ""); if( lasReferencias.size () % 10 _000_000 == 0){ System.out.println(n); try{ Thread.sleep (3000); } catch( InterruptedException e){} } }); } //... 25
  30. 30. Demo 2 - Referencias //... var lasReferencias = new ArrayList <Integer >(); var numeros = IntStream.iterate (1, n -> ++n); numeros.forEach(n -> { lasReferencias.add(n); if( lasReferencias.size () % 10 _000_000 == 0){ System.out.println(n); try{ Thread.sleep (3000); } catch( InterruptedException e){} } }); //... 26
  31. 31. Demo 3 - Concatenaci´on de String //... static String texto = ""; public static void main(String [] args ){ var numeros = IntStream.iterate (1, t -> ++t); numeros.forEach(n -> { texto += " " + n; if(n % 10 _000 == 0) System.out.println(n); }); } //... 27
  32. 32. Demo 4 - StringBuffer //... static StringBuilder texto = new StringBuilder(""); public static void main(String [] args ){ var numeros = IntStream.iterate (1, t -> ++t); numeros.forEach(n -> { texto.append(" "); texto.append(n); if(n % 10 _000 == 0) System.out.println(n); }); } //... 28
  33. 33. Complejidad de algoritmos
  34. 34. Complejidad Complejidad = Cantidad de pasos para realizar una tarea Figura 16: Complejidad computacional Tambi´en conocido como ”programar bien”:O 29
  35. 35. Fibonacci Figura 17: Sucesi´on Fibonacci 30
  36. 36. Demo 5 - Mal Fibonacci //... public static long doFibonacci(int n) { if (n <= 1) return n; else return doFibonacci(n-1) + doFibonacci(n -2); } //... 31
  37. 37. Demo 6 - Buen Fibonacci //... public static long doFibonacci(int n) { long a=0, b=1, c=0; for(int i = 0 ; i < n; i++){ c = a + b; a = b; b = c; } return c; } //... 32
  38. 38. Gracias • me@vorozco.com • http://vorozco.com • http://github.com/tuxtor/slides This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Guatemala License. 33

×