29
Finalizers –The not so Good, the Bad and the Ugly A Garbage Collection Saga Gautam Singh www.livemykarma.com @singhkgautam

Finalizers –The not so Good, the Bad and the Ugly

Embed Size (px)

Citation preview

Page 1: Finalizers –The not so Good, the Bad and the Ugly

Finalizers –The not so Good, the Bad and the Ugly

A Garbage Collection Saga

Gautam Singh www.livemykarma.com @singhkgautam

Page 2: Finalizers –The not so Good, the Bad and the Ugly

Fun Day

Page 3: Finalizers –The not so Good, the Bad and the Ugly

Work Day

Page 4: Finalizers –The not so Good, the Bad and the Ugly

Analyzing Thread Dump

Page 5: Finalizers –The not so Good, the Bad and the Ugly

What is Finalizer?

• Method on Object class. protected void finalize() throws Throwable

• Used to perform postmortem cleanup.

• Called by GC after an object is dead.

• Called by Finalizer thread

Page 6: Finalizers –The not so Good, the Bad and the Ugly

Finalizable Object life Cycle

Page 7: Finalizers –The not so Good, the Bad and the Ugly

Benchmark Results

Avg. time(ns) per Object creation

7X – 600X slow

Avg. Time(ms) spend in GC

50X – 200X more

Allocation(Bytes) Per Instance

Around 3X

Benchmarking library: JMH http://openjdk.java.net/projects/code-tools/jmh/ GitHub Repo: https://github.com/LiveMyKarma/Finalizer

Page 8: Finalizers –The not so Good, the Bad and the Ugly

Problem Class

Page 9: Finalizers –The not so Good, the Bad and the Ugly

Load Test Result (Before)

Page 10: Finalizers –The not so Good, the Bad and the Ugly

Load Test Result (After)

Page 11: Finalizers –The not so Good, the Bad and the Ugly

Problem with Finalizers

Finalizers are unpredictable, often dangerous, and generally unnecessary

Effective Java – Joshua Bloch

Page 12: Finalizers –The not so Good, the Bad and the Ugly

Dead Object Resurrection(Bad)

Page 13: Finalizers –The not so Good, the Bad and the Ugly

Dead Object Resurrection(Bad)

Page 14: Finalizers –The not so Good, the Bad and the Ugly

Increase Allocation Cost(Bad)

Allocation(Bytes) Per Instance

Around 3X

Page 15: Finalizers –The not so Good, the Bad and the Ugly

Cause more GCs(Ugly)

Page 16: Finalizers –The not so Good, the Bad and the Ugly

No Guarantee, when called (Ugly)

Page 17: Finalizers –The not so Good, the Bad and the Ugly

No Guarantee (Example)

Page 18: Finalizers –The not so Good, the Bad and the Ugly

Make object concurrent(Ugly)

Page 19: Finalizers –The not so Good, the Bad and the Ugly

When to use Finalizer(Not so Good)

• De-allocation of non-memory resources for objects

– With hard to predict lifetimes

– For which cleanup is not time-critical

• Safety net when client forget to call explicit clean-up method.

– Logging

Page 20: Finalizers –The not so Good, the Bad and the Ugly

How to avoid Finalizers Pitfalls

Page 21: Finalizers –The not so Good, the Bad and the Ugly

Do you really need finalizer?

Page 22: Finalizers –The not so Good, the Bad and the Ugly

Utilize Lifecycle methods, if available

Page 23: Finalizers –The not so Good, the Bad and the Ugly

Provide explicit termination method.

Page 24: Finalizers –The not so Good, the Bad and the Ugly

Reduce data overhead(Before)

Page 25: Finalizers –The not so Good, the Bad and the Ugly

Reduce data overhead(After)

Page 26: Finalizers –The not so Good, the Bad and the Ugly

Utilize Cache or Object pool

Image: http://www.birdsandblooms.com/birding/birding-basics/bird-behavior-caching-food/

Page 28: Finalizers –The not so Good, the Bad and the Ugly

I think my eyes are getting better. Instead of a big dark blur, I see a big light blur.

HAN SOLO, Star Wars Episode VI: Return of the Jedi

Page 29: Finalizers –The not so Good, the Bad and the Ugly

Questions?