1 . 1
JVM MemoryManagement &
Garbage Collection
2 . 1
Goals of this talk
2 . 2
It will focus on GC education
2 . 3
You will learn about thelifecycle of an object
2 . 4
Introduction to a JVMprofiler
3 . 1
Why do we care?
3 . 2
be able to reason aboutcode
3 . 3
responsive and fasterapplications = more profit
$$$
3 . 4
we all want to be more likeBatman
4 . 1
GC Terminology
4 . 2
1. Heap: memory allocatedby the JVM for all objects
4 . 3
2. Generational GarbageCollector
4 . 4
3. Collection: freeing up ofmemory
4 . 5
4. Object Promotion: anobject is moved from onepart of heap to another
4 . 64 . 7
5. Eden Space: new objectsallocated here
4 . 8
6. Survivor Space: tempstore for objects that havesurvived a young collection
4 . 9
7. Old Generation: long livedobjects live here
4 . 10
8. Minor Collection:collection in the young
generation, is fast
4 . 11
9. Major Collection:collection in the oldgeneration, is slow
4 . 12
10. GC Pause: time duringwhich a collection occurs,and the application code is
not running
4 . 13
11. Stop the World Event:application threads are
stopped until the operationcompletes
5 . 1
Concurrency VsParallelism
5 . 2
Concurrency is thecharacteristic of a set of
tasks, whereby they can beexecuted independently of
each other.
5 . 3
Parallelism is a way ofexecuting concurrent tasks,
on separate processors.
6 . 1
Different CollectorTypes
6 . 2
A Concurrent Collectorperforms garbage collection
concurrently with theapplication's own execution
6 . 3
A Parallel Collector usesmultiple CPUs to perform
garbage collection
7 . 1
Some GC limits
7 . 2
if we had infinite memory,we would never have to
collect, and GC would take0% of the CPU time.
7 . 3
if we had exactly 1 byte ofmemory, GC would take100% of the CPU time.
7 . 4
GC CPU utilization follows arough 1/x curve between
these 2 points
8 . 1
Common to allYoung generationGC mechanisms
8 . 2
identify live objects inmemory heap
8 . 3
reclaim resources held bydead objects
8 . 4
periodically relocate liveobjects via object
promotion
9 . 1
Minor Collection
9 . 2
occurs when the eden spacefills up
9 . 3
optimized assuming highobject mortality rate
9 . 4
survivor space divided into"to" space & "from" space
9 . 5
live objects from eden arecopied to the survivorspace, eden is freed.
9 . 6
surviving objects are agedand moved to old
generation
9 . 7
work of minor collectionproportional to num live
objects
9 . 8
all minor collections are"Stop the World" events
10 . 1
Major Collection
10 . 2
occurs when the oldgeneration is collected
10 . 3
triggered by minor GC
10 . 4
promotion failure fromyoung generation triggers
Full GC
10 . 5
Full GC cleans entire heap, isvery expensive and should
be avoided
10 . 6
old generation collectortries to predict when it
needs to collect to avoidpromotion failure
11 . 1
The Concurrent MarkSweep(CMS)
Collector
11 . 2
runs in the old generationcollecting tenured objects
no longer reachable
11 . 3
runs concurrently with theapplication
11 . 4
goal is to keep sufficientfree space in old gen so nopromotion failure occurs
12 . 1
CMS follows amultistep process
12 . 2
1. Initial Mark: find GC roots
12 . 3
2. Concurrent Mark: mark allreachable objects from GC
roots
12 . 4
3. Concurrent Pre-clean:�nd object references that have updated
�nd new objects that have been promotedduring the concurrent mark phase byremarking
12 . 5
4. Re-mark: Capture objectreferences that have been
update
12 . 6
5. Concurrent Sweep:update the free-lists by
reclaiming memory
13 . 1
Additional CMSObservations
13 . 2
CMS is not a compactingcollector, may result in old
gen fragmentationFull GC may be triggered because of
fragmentation
13 . 3
CMS is mostly concurrentwith the application, which
means -CPU time used for GC => less CPU time
available for application
13 . 4
CMS reduces the app'sthroughput, makes minor
collections more expensive
14 . 1
Profiler
14 . 2
connect to any running JVMprocess
14 . 3
can measure assumptions,and prove them
right/wrong
15 . 1
Demo !
16 . 1
Conclusion
16 . 2
garbage collection is hard
16 . 3
understand your tools, it willmake you a better
programmer
16 . 4
profile your application, youwill often be surprised
16 . 5
be more like Batman!
17 . 1
Thanks! Questions?
Top Related