jvm memory management & garbage collection

74
1.1 JVM Memory Management & Garbage Collection

Upload: punit-rathore

Post on 19-Feb-2017

109 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: JVM Memory Management & Garbage Collection

1 . 1

JVM MemoryManagement &

Garbage Collection

Page 2: JVM Memory Management & Garbage Collection

2 . 1

Goals of this talk

Page 3: JVM Memory Management & Garbage Collection

2 . 2

It will focus on GC education

Page 4: JVM Memory Management & Garbage Collection

2 . 3

You will learn about thelifecycle of an object

Page 5: JVM Memory Management & Garbage Collection

2 . 4

Introduction to a JVMprofiler

Page 6: JVM Memory Management & Garbage Collection

3 . 1

Why do we care?

Page 7: JVM Memory Management & Garbage Collection

3 . 2

be able to reason aboutcode

Page 8: JVM Memory Management & Garbage Collection

3 . 3

responsive and fasterapplications = more profit

$$$

Page 9: JVM Memory Management & Garbage Collection

3 . 4

we all want to be more likeBatman

Page 10: JVM Memory Management & Garbage Collection

4 . 1

GC Terminology

Page 11: JVM Memory Management & Garbage Collection

4 . 2

1. Heap: memory allocatedby the JVM for all objects

Page 12: JVM Memory Management & Garbage Collection

4 . 3

2. Generational GarbageCollector

Page 13: JVM Memory Management & Garbage Collection

4 . 4

3. Collection: freeing up ofmemory

Page 14: JVM Memory Management & Garbage Collection

4 . 5

4. Object Promotion: anobject is moved from onepart of heap to another

Page 15: JVM Memory Management & Garbage Collection
Page 16: JVM Memory Management & Garbage Collection

4 . 64 . 7

5. Eden Space: new objectsallocated here

Page 17: JVM Memory Management & Garbage Collection

4 . 8

6. Survivor Space: tempstore for objects that havesurvived a young collection

Page 18: JVM Memory Management & Garbage Collection

4 . 9

7. Old Generation: long livedobjects live here

Page 19: JVM Memory Management & Garbage Collection

4 . 10

8. Minor Collection:collection in the young

generation, is fast

Page 20: JVM Memory Management & Garbage Collection

4 . 11

9. Major Collection:collection in the oldgeneration, is slow

Page 21: JVM Memory Management & Garbage Collection

4 . 12

10. GC Pause: time duringwhich a collection occurs,and the application code is

not running

Page 22: JVM Memory Management & Garbage Collection

4 . 13

11. Stop the World Event:application threads are

stopped until the operationcompletes

Page 23: JVM Memory Management & Garbage Collection

5 . 1

Concurrency VsParallelism

Page 24: JVM Memory Management & Garbage Collection

5 . 2

Concurrency is thecharacteristic of a set of

tasks, whereby they can beexecuted independently of

each other.

Page 25: JVM Memory Management & Garbage Collection

5 . 3

Parallelism is a way ofexecuting concurrent tasks,

on separate processors.

Page 26: JVM Memory Management & Garbage Collection

6 . 1

Different CollectorTypes

Page 27: JVM Memory Management & Garbage Collection

6 . 2

A Concurrent Collectorperforms garbage collection

concurrently with theapplication's own execution

Page 28: JVM Memory Management & Garbage Collection

6 . 3

A Parallel Collector usesmultiple CPUs to perform

garbage collection

Page 29: JVM Memory Management & Garbage Collection

7 . 1

Some GC limits

Page 30: JVM Memory Management & Garbage Collection

7 . 2

if we had infinite memory,we would never have to

collect, and GC would take0% of the CPU time.

Page 31: JVM Memory Management & Garbage Collection

7 . 3

if we had exactly 1 byte ofmemory, GC would take100% of the CPU time.

Page 32: JVM Memory Management & Garbage Collection

7 . 4

GC CPU utilization follows arough 1/x curve between

these 2 points

Page 33: JVM Memory Management & Garbage Collection

8 . 1

Common to allYoung generationGC mechanisms

Page 34: JVM Memory Management & Garbage Collection

8 . 2

identify live objects inmemory heap

Page 35: JVM Memory Management & Garbage Collection

8 . 3

reclaim resources held bydead objects

Page 36: JVM Memory Management & Garbage Collection

8 . 4

periodically relocate liveobjects via object

promotion

Page 37: JVM Memory Management & Garbage Collection

9 . 1

Minor Collection

Page 38: JVM Memory Management & Garbage Collection

9 . 2

occurs when the eden spacefills up

Page 39: JVM Memory Management & Garbage Collection

9 . 3

optimized assuming highobject mortality rate

Page 40: JVM Memory Management & Garbage Collection

9 . 4

survivor space divided into"to" space & "from" space

Page 41: JVM Memory Management & Garbage Collection

9 . 5

live objects from eden arecopied to the survivorspace, eden is freed.

Page 42: JVM Memory Management & Garbage Collection

9 . 6

surviving objects are agedand moved to old

generation

Page 43: JVM Memory Management & Garbage Collection

9 . 7

work of minor collectionproportional to num live

objects

Page 44: JVM Memory Management & Garbage Collection

9 . 8

all minor collections are"Stop the World" events

Page 45: JVM Memory Management & Garbage Collection

10 . 1

Major Collection

Page 46: JVM Memory Management & Garbage Collection

10 . 2

occurs when the oldgeneration is collected

Page 47: JVM Memory Management & Garbage Collection

10 . 3

triggered by minor GC

Page 48: JVM Memory Management & Garbage Collection

10 . 4

promotion failure fromyoung generation triggers

Full GC

Page 49: JVM Memory Management & Garbage Collection

10 . 5

Full GC cleans entire heap, isvery expensive and should

be avoided

Page 50: JVM Memory Management & Garbage Collection

10 . 6

old generation collectortries to predict when it

needs to collect to avoidpromotion failure

Page 51: JVM Memory Management & Garbage Collection

11 . 1

The Concurrent MarkSweep(CMS)

Collector

Page 52: JVM Memory Management & Garbage Collection

11 . 2

runs in the old generationcollecting tenured objects

no longer reachable

Page 53: JVM Memory Management & Garbage Collection

11 . 3

runs concurrently with theapplication

Page 54: JVM Memory Management & Garbage Collection

11 . 4

goal is to keep sufficientfree space in old gen so nopromotion failure occurs

Page 55: JVM Memory Management & Garbage Collection

12 . 1

CMS follows amultistep process

Page 56: JVM Memory Management & Garbage Collection

12 . 2

1. Initial Mark: find GC roots

Page 57: JVM Memory Management & Garbage Collection

12 . 3

2. Concurrent Mark: mark allreachable objects from GC

roots

Page 58: JVM Memory Management & Garbage Collection

12 . 4

3. Concurrent Pre-clean:�nd object references that have updated

�nd new objects that have been promotedduring the concurrent mark phase byremarking

Page 59: JVM Memory Management & Garbage Collection

12 . 5

4. Re-mark: Capture objectreferences that have been

update

Page 60: JVM Memory Management & Garbage Collection

12 . 6

5. Concurrent Sweep:update the free-lists by

reclaiming memory

Page 61: JVM Memory Management & Garbage Collection

13 . 1

Additional CMSObservations

Page 62: JVM Memory Management & Garbage Collection

13 . 2

CMS is not a compactingcollector, may result in old

gen fragmentationFull GC may be triggered because of

fragmentation

Page 63: JVM Memory Management & Garbage Collection

13 . 3

CMS is mostly concurrentwith the application, which

means -CPU time used for GC => less CPU time

available for application

Page 64: JVM Memory Management & Garbage Collection

13 . 4

CMS reduces the app'sthroughput, makes minor

collections more expensive

Page 65: JVM Memory Management & Garbage Collection

14 . 1

Profiler

Page 66: JVM Memory Management & Garbage Collection

14 . 2

connect to any running JVMprocess

Page 67: JVM Memory Management & Garbage Collection

14 . 3

can measure assumptions,and prove them

right/wrong

Page 68: JVM Memory Management & Garbage Collection

15 . 1

Demo !

Page 69: JVM Memory Management & Garbage Collection

16 . 1

Conclusion

Page 70: JVM Memory Management & Garbage Collection

16 . 2

garbage collection is hard

Page 71: JVM Memory Management & Garbage Collection

16 . 3

understand your tools, it willmake you a better

programmer

Page 72: JVM Memory Management & Garbage Collection

16 . 4

profile your application, youwill often be surprised

Page 73: JVM Memory Management & Garbage Collection

16 . 5

be more like Batman!

Page 74: JVM Memory Management & Garbage Collection

17 . 1

Thanks! Questions?