(codemotion 2014) jvm gc: wtf?!

102
JVM GC WTF?! Alonso Torres @alotor

Upload: alonso-torres

Post on 09-Jul-2015

2.025 views

Category:

Technology


7 download

DESCRIPTION

La máquina virtual y la plataforma Java se está convirtiendo en el pilar de multiples lenguajes: Java, Scala, Groovy, Clojure, Ceylon, JRuby... pero muchos desarrolladores no conocen como funciona por dentro tanto el sistema de memoria como el recolector de basura. Este conocimiento es fundamental y puede marcar la diferencia entre dos programadores. Esta charla consisitirá en una introducción a la gestión de memoria de la JVM (Hotspot), cómo trabaja el recolector de basura o algunas opciones para poder configurar las opciones por defecto. Si alguna vez te has cruzado con un OutOfMemoryError y no entendistes la línea que copiastes de StackOverflow: esta es tu charla.

TRANSCRIPT

Page 1: (Codemotion 2014) JVM GC: WTF?!

JVM GCWTF?!

Alonso Torres @alotor

Page 2: (Codemotion 2014) JVM GC: WTF?!

Alonso Torres

@alotor @alotor

mobro.co/alotor

Page 3: (Codemotion 2014) JVM GC: WTF?!
Page 4: (Codemotion 2014) JVM GC: WTF?!

java.lang.OutOfMemoryError: unable to create new native thread

java.lang.OutOfMemoryError: Stack overflow

java.lang.OutOfMemoryError: PermGen space

java.lang.OutOfMemoryError: Metaspace

java.lang.OutOfMemoryError: Java heap space

java.lang.OutOfMemoryError: GC overhead limit exceeded

java.lang.OutOfMemoryError: requested... Out of swap space?

Page 5: (Codemotion 2014) JVM GC: WTF?!
Page 6: (Codemotion 2014) JVM GC: WTF?!

SUCCESS

Page 7: (Codemotion 2014) JVM GC: WTF?!

No tengo memoria!!!

Todo va lento!!!!!!

El GC es ineficiente!!!!!

Page 8: (Codemotion 2014) JVM GC: WTF?!

With CMS GC, the full collection is serial and STW, hence your application threads are stopped for the entire duration while the heap space is reclaimed and then compacted.

The duration for the STW pause depends on your heap size and the surviving objects.

http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

Page 9: (Codemotion 2014) JVM GC: WTF?!

Con el recolector CMS, la recolección es “serie” y STW, y los hilos de tu aplicación serán detenidos por la duración completa mientras el espacio del “heap” es recuperado y compactado.

La duración de la pausa STW dependerá del tamaño del “heap” y de los objetos supervivientes.

http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

Page 10: (Codemotion 2014) JVM GC: WTF?!
Page 11: (Codemotion 2014) JVM GC: WTF?!

JVMJava Virtual Machine

GCGarbage Collectors

WTF?!Why They Fail?!

Page 12: (Codemotion 2014) JVM GC: WTF?!

1. Estructura de la memoria

2. Conceptos generales de GC

3. Tipos de recolectores de Hotspot

4. Configuración de Hotspot

Page 13: (Codemotion 2014) JVM GC: WTF?!

Os presento la

JVM

Page 14: (Codemotion 2014) JVM GC: WTF?!

JVM PROCESS: 1.591 Gb

Page 15: (Codemotion 2014) JVM GC: WTF?!

JVM PROCESS: 1.591 Gb

Memoria propia de la JVM

Page 16: (Codemotion 2014) JVM GC: WTF?!

JVM PROCESS: 1.591 Gb

Memoria de ejecución

Page 17: (Codemotion 2014) JVM GC: WTF?!

JVM PROCESS: 1.591 Gb

Memoria de datos

Page 18: (Codemotion 2014) JVM GC: WTF?!

JVM PROCESS: 1.591 Gb

Memoria de clases

Page 19: (Codemotion 2014) JVM GC: WTF?!

HEAP

- Bloque de memoria que la JVM se encargará de gestionar

- Almacena los datos de los objetos

- Memoria dinámica

Page 20: (Codemotion 2014) JVM GC: WTF?!

LIBREOCUPADA

Max. Heap Size

Page 21: (Codemotion 2014) JVM GC: WTF?!

LIBREOCUPADA

Max. Heap Size

Page 22: (Codemotion 2014) JVM GC: WTF?!

LIBREOCUPADA

Max. Heap Size

Page 23: (Codemotion 2014) JVM GC: WTF?!

LIBREOCUPADA

Page 24: (Codemotion 2014) JVM GC: WTF?!

LIBREOCUPADA

Page 25: (Codemotion 2014) JVM GC: WTF?!

AUMENTAMOS el heap

ó

LIBERAMOS memoria

Page 26: (Codemotion 2014) JVM GC: WTF?!

OCUPADA LIBRE

OCUPADA LIBRE

Aumenta el heap

Liberamos el heap

Page 27: (Codemotion 2014) JVM GC: WTF?!

¿Cómo liberamos la memoriaque ya no necesitamos?

Page 28: (Codemotion 2014) JVM GC: WTF?!

1. Busca todos los objetos vivos

2. Libera la memoria de los objetos muertos

3. Mueve la posición de memoria de los vivos

Garbage Collection

Page 29: (Codemotion 2014) JVM GC: WTF?!

- Serial Collector

- Parallel Collector

- CMS Collector

- G1

GC’s en OpenJDK Hotspot

Page 30: (Codemotion 2014) JVM GC: WTF?!

- Serial Collector

- Parallel Collector

- CMS Collector

- G1

GC’s en OpenJDK Hotspot

Page 31: (Codemotion 2014) JVM GC: WTF?!

MALDITOS TÉRMINOS!!

Page 32: (Codemotion 2014) JVM GC: WTF?!

- Parada global de todos los hilos de ejecución para realizar recolección de basura

- El contrario puede ser CONCURRENTE○ La recolección se ejecuta a la vez que el

programa

Stop the World (STW)

Page 33: (Codemotion 2014) JVM GC: WTF?!

STW Concurrente

Page 34: (Codemotion 2014) JVM GC: WTF?!

● La ejecución se realiza en varios hilos de ejecución

● Aprovecha sistemas con múltiples CPUs

● Por contra puede ser SERIE○ Sólo se ejecutaría en un hilo

Paralelo

Page 35: (Codemotion 2014) JVM GC: WTF?!

Paralelo Serie

Page 36: (Codemotion 2014) JVM GC: WTF?!

● El trabajo de recolección no se realiza en un único paso sino en varias fases o pasos

● Por el contrario puede ser MONOLÍTICO○ Todo se ejecuta en un sólo paso

Incremental

Page 37: (Codemotion 2014) JVM GC: WTF?!

Incremental Monolítico

Page 38: (Codemotion 2014) JVM GC: WTF?!

- La mayoría de los objetos mueren jóvenes

- Los objetos viejos no suelen referenciar a objetos jóvenes

Weak Generational Hypothesis

Page 39: (Codemotion 2014) JVM GC: WTF?!

Dos tipos de recolección:

○ 1 espacio pequeño con muchos muertos

○ 1 espacio grande donde casi todos vivos

Generational GC

Page 40: (Codemotion 2014) JVM GC: WTF?!

Núm

ero

de o

bjet

os v

ivos

Número de objetos

Page 41: (Codemotion 2014) JVM GC: WTF?!

TODOSlos GC’s son generacionales

Page 42: (Codemotion 2014) JVM GC: WTF?!

YOUNG OLD/TENURE

Generational heap structure

Page 43: (Codemotion 2014) JVM GC: WTF?!

SURVIVORS

OLD/TENUREEDEN

Generational heap structure

SURVIVORS

Page 44: (Codemotion 2014) JVM GC: WTF?!
Page 45: (Codemotion 2014) JVM GC: WTF?!
Page 46: (Codemotion 2014) JVM GC: WTF?!

Recolectamos la Young Generation

Objetos promocionados van a supervivientes y a la Old Generation

Page 47: (Codemotion 2014) JVM GC: WTF?!
Page 48: (Codemotion 2014) JVM GC: WTF?!

La Old Generation está llena

Necesitamos una recolección completa

Page 49: (Codemotion 2014) JVM GC: WTF?!

Empty young and survivors.

Free dead old-gen objects

Page 50: (Codemotion 2014) JVM GC: WTF?!

● Serial Collector

● Parallel Collector

● CMS Collector

● G1 (Garbage First) Collector

GC’s en OpenJDK Hotspot

Page 51: (Codemotion 2014) JVM GC: WTF?!

YOUNG GENERATION OLD GENERATION

Serial

Monolithic

Stop-The-World

Copying Mark / Sweep / Compact

Serial Collector

Page 52: (Codemotion 2014) JVM GC: WTF?!

- Copia los objetos vivos de una región de la memoria a otra

- Libera la zona de memoria antigua

Algoritmo de COPIA (Scavenge)

Page 53: (Codemotion 2014) JVM GC: WTF?!

Mark / Sweep / Compact (MSC)

1. Mark

Page 54: (Codemotion 2014) JVM GC: WTF?!

2. Sweep

Mark / Sweep / Compact (MSC)

Page 55: (Codemotion 2014) JVM GC: WTF?!

3. Compact

Mark / Sweep / Compact (MSC)

Page 56: (Codemotion 2014) JVM GC: WTF?!

● Serial Collector

● Parallel Collector

● CMS Collector

● G1 (Garbage First) Collector

GC’s en OpenJDK Hotspot

Page 57: (Codemotion 2014) JVM GC: WTF?!

Parallel Collector

YOUNG GENERATION OLD GENERATION

Paralelo Serie / Paralelo

Monolítico

Stop-The-World

Copying Mark / Sweep / Compact

Page 58: (Codemotion 2014) JVM GC: WTF?!

● Serial Collector

● Parallel Collector

● CMS Collector

● G1 (Garbage First) Collector

GC’s en OpenJDK Hotspot

Page 59: (Codemotion 2014) JVM GC: WTF?!

YOUNG GENERATION OLD GENERATION

Paralelo Serie Y Paralelo

Monolítico Incremental

Stop-The-World STW Y Concurrente

Copying Mark and Sweep

Concurrent Mark & Sweep

Page 60: (Codemotion 2014) JVM GC: WTF?!

1. Initial Mark

2. Concurrent Mark

3. Remark

4. Concurrent Sweep

Concurrent Mark & Sweep

Page 61: (Codemotion 2014) JVM GC: WTF?!

CMSNo compacta

Page 62: (Codemotion 2014) JVM GC: WTF?!

CMS después de varias generaciones

Page 63: (Codemotion 2014) JVM GC: WTF?!

Cuando hay excesiva

fragmentación

STW,Serial, Monolithic, MSC*

*AKA: Full GC

Page 64: (Codemotion 2014) JVM GC: WTF?!

● Serial Collector

● Parallel Collector

● CMS Collector

● G1 (Garbage First) Collector

GC’s en OpenJDK Hotspot

Page 65: (Codemotion 2014) JVM GC: WTF?!

- Retrasar al máximo un “Full GC”

- Baja latencia

- Predictibilidad

- Fácil de usar

Garbage First (G1)

Page 66: (Codemotion 2014) JVM GC: WTF?!

- Divide el heap en regiones

- Libera primero las que tienen más basura (Garbage First)

- Compacta sobre la marcha

Garbage First (G1)

Page 67: (Codemotion 2014) JVM GC: WTF?!

E O

O

E

O

O

O

S

G1 Heap Structure

E

Page 68: (Codemotion 2014) JVM GC: WTF?!

1. Recolección de Young Collection

2. Marcado concurrente

3. Recolección mixta

4. Full GC

Garbage First (G1)

Page 69: (Codemotion 2014) JVM GC: WTF?!

Young collection

E O

O

E E

O

O

O

E

Page 70: (Codemotion 2014) JVM GC: WTF?!

Young collection

O

O

O

O

O

S

Page 71: (Codemotion 2014) JVM GC: WTF?!

1. Recolección de Young Collection

2. Marcado concurrente

3. Recolección mixta

4. Full GC

Garbage First (G1)

Page 72: (Codemotion 2014) JVM GC: WTF?!

Marcado concurrente

E O

O

O

O

O

E

O

O O

E S

Page 73: (Codemotion 2014) JVM GC: WTF?!

Marcado concurrente

E O

O

E

X

O

X

O

O O

O

Page 74: (Codemotion 2014) JVM GC: WTF?!

1. Recolección de Young Collection

2. Marcado concurrente

3. Recolección mixta

4. Full GC

Garbage First (G1)

Page 75: (Codemotion 2014) JVM GC: WTF?!

Recolección mixta

E O

O

E

E X

O

X

E

O

O O

O

O

O

O

O

Page 76: (Codemotion 2014) JVM GC: WTF?!

Recolección mixta

E O

O

E

E X

O

X

E

O

O O

O

O

O

O

O

Page 77: (Codemotion 2014) JVM GC: WTF?!

Recolección mixta

O

O

O

X

O

O O

O

O

O

O

OS

Page 78: (Codemotion 2014) JVM GC: WTF?!

1. Recolección de Young Collection

2. Marcado concurrente

3. Recolección mixta

4. Full GC

Garbage First (G1)

Page 79: (Codemotion 2014) JVM GC: WTF?!

Todos los GC’s de Hotspottienen algún tipo de STW

Page 80: (Codemotion 2014) JVM GC: WTF?!

STWes el mayor enemigo

del rendimiento

Page 81: (Codemotion 2014) JVM GC: WTF?!

1. Maximizar recolección JOVEN

2. Minimizar los STW

3. Evitar objetos grandes

4. Evitar retención de objetos

Objetivos de optimización

Page 82: (Codemotion 2014) JVM GC: WTF?!

Monitorizar la aplicación

Page 83: (Codemotion 2014) JVM GC: WTF?!

-Xloggc:<file>

-XX:+PrintGCDetails

-XX:+PrintGCTimeStamps

Opciones de monitorización

Page 84: (Codemotion 2014) JVM GC: WTF?!

[GC [PSYoungGen: 578424K->8793K(630784K)]

1007570K->444386K(1155072K), 0.0185270 secs]

[Times: user=0.06 sys=0.00, real=0.02 secs]

[GC [PSYoungGen: 580697K->15128K(636928K)]

1016290K->459169K(1161216K), 0.0236090 secs]

[Times: user=0.08 sys=0.01, real=0.02 secs]

[GC [PSYoungGen: 450179K->6893K(635904K)]

894221K->465458K(1160192K), 0.0249430 secs]

[Times: user=0.07 sys=0.02, real=0.02 secs]

[Full GC [PSYoungGen: 6893K->0K(635904K)]

[ParOldGen: 458564K->454236K(816128K)] 465458K->454236K(1452032K)

[PSPermGen: 171991K->171852K(344064K)], 2.5341620 secs]

[Times: user=8.48 sys=0.01, real=2.53 secs]

Page 85: (Codemotion 2014) JVM GC: WTF?!

[GC [PSYoungGen: 578424K->8793K(630784K)]

1007570K->444386K(1155072K), 0.0185270 secs]

[Times: user=0.06 sys=0.00, real=0.02 secs]

[GC [PSYoungGen: 580697K->15128K(636928K)]

1016290K->459169K(1161216K), 0.0236090 secs]

[Times: user=0.08 sys=0.01, real=0.02 secs]

[GC [PSYoungGen: 450179K->6893K(635904K)]

894221K->465458K(1160192K), 0.0249430 secs]

[Times: user=0.07 sys=0.02, real=0.02 secs]

[Full GC [PSYoungGen: 6893K->0K(635904K)]

[ParOldGen: 458564K->454236K(816128K)] 465458K->454236K(1452032K)

[PSPermGen: 171991K->171852K(344064K)], 2.5341620 secs]

[Times: user=8.48 sys=0.01, real=2.53 secs]

Recolección Young Generation

Full GC (STW)

Page 86: (Codemotion 2014) JVM GC: WTF?!
Page 87: (Codemotion 2014) JVM GC: WTF?!

YOUNG SURVIVORS OLD

Resumen del total

Page 88: (Codemotion 2014) JVM GC: WTF?!

Análisis en detalle

Page 89: (Codemotion 2014) JVM GC: WTF?!

jcmd pid GC.class_histogram

Herramientas

Page 90: (Codemotion 2014) JVM GC: WTF?!

num #instances #bytes class name---------------------------------------------- 1: 762525 71798392 [C 2: 41739 68376080 [B 3: 675097 54007760 java.lang.reflect.Method 4: 404377 51770560 <methodKlass> 5: 404377 49495808 <constMethodKlass> 6: 864167 48393352 org.codehaus.groovy.runtime.metaclass. 7: 17110 29966392 <constantPoolKlass> 8: 710424 22733568 java.util.HashMap$Entry 9: 13777 18368640 <constantPoolCacheKlass> 10: 760859 18260616 java.lang.String 11: 507462 15419000 [Ljava.lang.Object; 12: 17104 14833688 <instanceKlassKlass> 13: 85375 12536048 [Lorg.codehaus.groovy.util.ComplexKeyHashMap$En 14: 303170 9701440 org.codehaus.groovy.util.SingleKeyHashMap$Entry 15: 386458 9274992 org.codehaus.groovy.util.FastArray 16: 50174 8596088 [Ljava.util.HashMap$Entry; 17: 333114 7010016 [Ljava.lang.Class;

Page 91: (Codemotion 2014) JVM GC: WTF?!

jcmd pid GC.class_histogram

jcmd pid GC.heap_dump filename +jhat heapdumpfile

Herramientas

Page 92: (Codemotion 2014) JVM GC: WTF?!
Page 93: (Codemotion 2014) JVM GC: WTF?!

-XX:+HeapDumpOnOutOfMemoryError

-XX:HeapDumpPath=path

Herramientas

Page 94: (Codemotion 2014) JVM GC: WTF?!

Configurar la JVM

Page 95: (Codemotion 2014) JVM GC: WTF?!

-Xmx / -XmsTamaño máximo y mínimo del HEAP

-XX:MaxGCPauseMillis=nRecomendación de pausas

-XX:SurvivorRatio=nTamaño del espacio de supervivientes

Configuraciones

Page 96: (Codemotion 2014) JVM GC: WTF?!

Tantas como para otra charla ;)

Page 97: (Codemotion 2014) JVM GC: WTF?!
Page 98: (Codemotion 2014) JVM GC: WTF?!

1. Estructura de la memoria

2. Conceptos generales de GC

3. Tipos de recolectores de Hotspot

4. Configuración de Hotspot

Page 99: (Codemotion 2014) JVM GC: WTF?!

Con el recolector CMS, la recolección es “serie” y STW, y los hilos de tu aplicación serán detenidos por la duración completa mientras el espacio del “heap” es recuperado y compactado.

La duración de la pausa STW dependerá del tamaño del “heap” y de los objetos supervivientes.

http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

Page 100: (Codemotion 2014) JVM GC: WTF?!

No es tan fiero el lobo como lo pintan

Page 101: (Codemotion 2014) JVM GC: WTF?!

Alonso Torres

@alotor @alotor

mobro.co/alotor

Page 102: (Codemotion 2014) JVM GC: WTF?!

Bonus (post-presentación)

- Alternativas a JConsole○ VisualVM○ AppDynamics

- Para medir paradas del GC○ JHiccup http://www.azulsystems.com/product/jHiccup○ JMeter