SlideShare a Scribd company logo
Introducci´on al GC en Java - Porqu´e mis
aplicaciones explotan
V´ıctor Orozco - @tuxtor
10 de mayo de 2018
GuateJUG
1
V´ıctor Orozco
• Developer (JVM/Open
Source Advocate)
• JUG Leader
• Consultor independiente
• Profesor universitario
• @tuxtor
• http://vorozco.com
• http://tuxtor.shekalug.org
2
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
Memoria en Java
Stack vs heap
Figura 1: Stack y Heap
4
Stack vs heap
Figura 2: Java main thread
5
Mark and sweep
Figura 3: Mark and sweep, Credits: https://plumbr.io
6
Mark and sweep
Figura 4: Mark and sweep - Mark, Credits: https://plumbr.io
Generalmente DFS
7
Mark and sweep
Figura 5: Mark and sweep - sweep, Credits: https://plumbr.io
8
Mark and sweep
Figura 6: Mark - sweep, Credits: https://plumbr.io
9
Mark - sweep - compact
Figura 7: Mark - sweep - compact, Credits: https://plumbr.io
10
Mark and copy
Figura 8: Mark and copy, Credits: https://plumbr.io
11
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
Generational garbage collectors
Generational GC(HotSpot)
Figura 9: Generational GC, Credits: Oracle
13
Generational GC(HotSpot)
Figura 10: Generational GC, Credits: Oracle
14
Generational GC(HotSpot)
Figura 11: Generational GC, Credits: Oracle
15
Generational GC(HotSpot)
Figura 12: Generational GC, Credits: Oracle
16
Generational GC(HotSpot)
Figura 13: Generational GC, Credits: Oracle
17
Generational GC(HotSpot)
Figura 14: Generational GC, Credits: Oracle
18
Generational GC(HotSpot)
Figura 15: Generational GC, Credits: Oracle
19
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
GC en HotSpot
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
SerialGC
• Young: Mark-Copy
• Old: Mark-Sweep-Compact
• java − XX : +UseSerialGCcom.nabenik.MyExecClass
22
ParallelGC
• Young: Mark-Copy
• Old: Mark-Sweep-Compact
• Stop-the-world en ambas regiones
• java − XX : +UseParallelGCcom.nabenik.MyExecClass
23
CMS
• Young: Mark-Copy - Stop the world
• Old: (Mostly )Concurrent Mark Sweep (Paralelo)
• java − XX :
+UseConcMarkSweepGCcom.nabenik.MyExecClass
24
Como luchar CONTRA un Garbage
Collector
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
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
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
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
Complejidad de algoritmos
Complejidad
Complejidad = Cantidad de pasos para realizar una tarea
Figura 16: Complejidad computacional
Tambi´en conocido como ”programar bien”:O
29
Fibonacci
Figura 17: Sucesi´on Fibonacci
30
Demo 5 - Mal Fibonacci
//...
public static long doFibonacci(int n) {
if (n <= 1)
return n;
else
return doFibonacci(n-1) + doFibonacci(n -2);
}
//...
31
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
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

More Related Content

PDF
Eclipse MicroProfile para el desarrollador ocupado
PDF
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
PDF
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
PDF
Eclipse MicroProfile metrics: Practical use cases
PDF
Eclipse MicroProfile para o desenvolvedor ocupado
PDF
De Java 8 ate Java 14
PDF
De Java 8 a Java 11 y 14
PDF
De Java 8 a Java 17
Eclipse MicroProfile para el desarrollador ocupado
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Eclipse MicroProfile metrics: Practical use cases
Eclipse MicroProfile para o desenvolvedor ocupado
De Java 8 ate Java 14
De Java 8 a Java 11 y 14
De Java 8 a Java 17

What's hot (20)

PDF
Explore the history, versions and features of Java- a report by Pranav Mishra
PDF
Design Patterns para Microsserviços com MicroProfile
PDF
Embedded systems
PPTX
Rx java in action
PDF
jcmd #javacasual
PDF
Building Scalable Stateless Applications with RxJava
PDF
Dropwizard
PPTX
7 jvm-arguments-v1
PDF
Introduction to Retrofit and RxJava
PDF
How to cook lettuce @Java casual
PPTX
Vert.x v3 - high performance polyglot application toolkit
PDF
[231] the simplicity of cluster apps with circuit
PPTX
Micro-metrics to forecast performance tsunamis
PPTX
16 artifacts to capture when there is a production problem
PPTX
Lets crash-applications
PPTX
Lets crash-applications
PPTX
Java On Speed
PPTX
Supercharged java 8 : with cyclops-react
PDF
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
PPTX
Don't dump thread dumps
Explore the history, versions and features of Java- a report by Pranav Mishra
Design Patterns para Microsserviços com MicroProfile
Embedded systems
Rx java in action
jcmd #javacasual
Building Scalable Stateless Applications with RxJava
Dropwizard
7 jvm-arguments-v1
Introduction to Retrofit and RxJava
How to cook lettuce @Java casual
Vert.x v3 - high performance polyglot application toolkit
[231] the simplicity of cluster apps with circuit
Micro-metrics to forecast performance tsunamis
16 artifacts to capture when there is a production problem
Lets crash-applications
Lets crash-applications
Java On Speed
Supercharged java 8 : with cyclops-react
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
Don't dump thread dumps
Ad

Similar to Principios básicos de Garbage Collector en Java (20)

PDF
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
PDF
Solr Troubleshooting - TreeMap approach
PDF
JRuby, Ruby, Rails and You on the Cloud
PDF
Microsoft Power Point Best Practices For Scaling Heavily Adopted And Concur...
PPTX
Gopher in performance_tales_ms_go_cracow
PPTX
Grow and Shrink - Dynamically Extending the Ruby VM Stack
PPT
Heap & thread dump
PDF
Microservices with Micronaut
PDF
Fight with Metaspace OOM
PDF
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
PPTX
Hadoop cluster performance profiler
PPTX
iSoligorsk #3 2013
PDF
Microservices with Micronaut
PDF
Nodejs性能分析优化和分布式设计探讨
PDF
Optimal Strategies for Large Scale Batch ETL Jobs with Emma Tang
PPTX
Optimal Strategies for Large-Scale Batch ETL Jobs
PPTX
Top 5 Java Performance Problems Presentation!
PPTX
Jvm problem diagnostics
PPTX
Are you ready for cloud-native java JavaCro2019
PDF
Profiling your Java Application
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Solr Troubleshooting - TreeMap approach
JRuby, Ruby, Rails and You on the Cloud
Microsoft Power Point Best Practices For Scaling Heavily Adopted And Concur...
Gopher in performance_tales_ms_go_cracow
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Heap & thread dump
Microservices with Micronaut
Fight with Metaspace OOM
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
Hadoop cluster performance profiler
iSoligorsk #3 2013
Microservices with Micronaut
Nodejs性能分析优化和分布式设计探讨
Optimal Strategies for Large Scale Batch ETL Jobs with Emma Tang
Optimal Strategies for Large-Scale Batch ETL Jobs
Top 5 Java Performance Problems Presentation!
Jvm problem diagnostics
Are you ready for cloud-native java JavaCro2019
Profiling your Java Application
Ad

More from Víctor Leonel Orozco López (20)

PDF
Introducción al análisis de datos
PDF
From traditional to GitOps
PDF
Iniciando microservicios reales con JakartaEE/MicroProfile y arquetipos de Maven
PDF
Desde la TV, hasta la nube, el ecosistema de Java en 26 años
PDF
Bootstraping real world Jakarta EE/MicroProfile microservices with Maven Arch...
PDF
Tolerancia a fallas, service mesh y chassis
PDF
Explorando los objetos centrales de Kubernetes con Oracle Cloud
PDF
Introducción a GraalVM Native para aplicaciones JVM
PDF
Desarrollo moderno con DevOps y Cloud Native
PDF
Gestión de proyectos con Maven
PDF
MicroProfile benefits for your monolithic applications
PDF
Actualizando aplicaciones empresariales en Java desde Java 8 on premise hasta...
PDF
Actualizando aplicaciones empresariales en Java desde Java 8 on premise hasta...
PDF
Consejos y el camino del desarrollador de software
PDF
Seguridad de aplicaciones Java/JakartaEE con OWASP Top 10
PDF
Introducción a Kotlin para desarrolladores Java
PDF
Programación con ECMA6 y TypeScript
PDF
Empaquetando aplicaciones Java con Docker y Kubernetes
PDF
MicroProfile benefits for monolitic applications
PDF
Microservicios con Jakarta EE y Eclipse MicroProfile
Introducción al análisis de datos
From traditional to GitOps
Iniciando microservicios reales con JakartaEE/MicroProfile y arquetipos de Maven
Desde la TV, hasta la nube, el ecosistema de Java en 26 años
Bootstraping real world Jakarta EE/MicroProfile microservices with Maven Arch...
Tolerancia a fallas, service mesh y chassis
Explorando los objetos centrales de Kubernetes con Oracle Cloud
Introducción a GraalVM Native para aplicaciones JVM
Desarrollo moderno con DevOps y Cloud Native
Gestión de proyectos con Maven
MicroProfile benefits for your monolithic applications
Actualizando aplicaciones empresariales en Java desde Java 8 on premise hasta...
Actualizando aplicaciones empresariales en Java desde Java 8 on premise hasta...
Consejos y el camino del desarrollador de software
Seguridad de aplicaciones Java/JakartaEE con OWASP Top 10
Introducción a Kotlin para desarrolladores Java
Programación con ECMA6 y TypeScript
Empaquetando aplicaciones Java con Docker y Kubernetes
MicroProfile benefits for monolitic applications
Microservicios con Jakarta EE y Eclipse MicroProfile

Recently uploaded (20)

PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Getting Started with Data Integration: FME Form 101
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
August Patch Tuesday
PDF
Encapsulation theory and applications.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Zenith AI: Advanced Artificial Intelligence
Getting Started with Data Integration: FME Form 101
A comparative analysis of optical character recognition models for extracting...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
1. Introduction to Computer Programming.pptx
DP Operators-handbook-extract for the Mautical Institute
Tartificialntelligence_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Group 1 Presentation -Planning and Decision Making .pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Hybrid model detection and classification of lung cancer
Enhancing emotion recognition model for a student engagement use case through...
August Patch Tuesday
Encapsulation theory and applications.pdf
TLE Review Electricity (Electricity).pptx
WOOl fibre morphology and structure.pdf for textiles
MIND Revenue Release Quarter 2 2025 Press Release
gpt5_lecture_notes_comprehensive_20250812015547.pdf

Principios básicos de Garbage Collector en Java

  • 1. Introducci´on al GC en Java - Porqu´e mis aplicaciones explotan V´ıctor Orozco - @tuxtor 10 de mayo de 2018 GuateJUG 1
  • 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. 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
  • 5. Stack vs heap Figura 1: Stack y Heap 4
  • 6. Stack vs heap Figura 2: Java main thread 5
  • 7. Mark and sweep Figura 3: Mark and sweep, Credits: https://plumbr.io 6
  • 8. Mark and sweep Figura 4: Mark and sweep - Mark, Credits: https://plumbr.io Generalmente DFS 7
  • 9. Mark and sweep Figura 5: Mark and sweep - sweep, Credits: https://plumbr.io 8
  • 10. Mark and sweep Figura 6: Mark - sweep, Credits: https://plumbr.io 9
  • 11. Mark - sweep - compact Figura 7: Mark - sweep - compact, Credits: https://plumbr.io 10
  • 12. Mark and copy Figura 8: Mark and copy, Credits: https://plumbr.io 11
  • 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
  • 15. Generational GC(HotSpot) Figura 9: Generational GC, Credits: Oracle 13
  • 16. Generational GC(HotSpot) Figura 10: Generational GC, Credits: Oracle 14
  • 17. Generational GC(HotSpot) Figura 11: Generational GC, Credits: Oracle 15
  • 18. Generational GC(HotSpot) Figura 12: Generational GC, Credits: Oracle 16
  • 19. Generational GC(HotSpot) Figura 13: Generational GC, Credits: Oracle 17
  • 20. Generational GC(HotSpot) Figura 14: Generational GC, Credits: Oracle 18
  • 21. Generational GC(HotSpot) Figura 15: Generational GC, Credits: Oracle 19
  • 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
  • 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. SerialGC • Young: Mark-Copy • Old: Mark-Sweep-Compact • java − XX : +UseSerialGCcom.nabenik.MyExecClass 22
  • 26. ParallelGC • Young: Mark-Copy • Old: Mark-Sweep-Compact • Stop-the-world en ambas regiones • java − XX : +UseParallelGCcom.nabenik.MyExecClass 23
  • 27. CMS • Young: Mark-Copy - Stop the world • Old: (Mostly )Concurrent Mark Sweep (Paralelo) • java − XX : +UseConcMarkSweepGCcom.nabenik.MyExecClass 24
  • 28. Como luchar CONTRA un Garbage Collector
  • 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. 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. 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. 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
  • 34. Complejidad Complejidad = Cantidad de pasos para realizar una tarea Figura 16: Complejidad computacional Tambi´en conocido como ”programar bien”:O 29
  • 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. 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. 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