O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

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

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Finalize() method
Finalize() method
Carregando em…3
×

Confira estes a seguir

1 de 29 Anúncio

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

Baixar para ler offline

Slides from my talk @ DevoxxUS 2017.
Finalizer (the Object.finalize() method) is a construct in Java which is as old as Java itself, still only few are aware of its huge side effects. In this talk we will discuss why finalizers are not always a good choice, its overhead on Garbage Collector and how it can be avoided. Using simple JMH benchmark I will demonstrate the performance overhead of finalizers.

We will also talk about my firsthand experience with finalizers where just by removing finalizers I was able to fix major performance issue in a webapp for a large retailer.

Slides from my talk @ DevoxxUS 2017.
Finalizer (the Object.finalize() method) is a construct in Java which is as old as Java itself, still only few are aware of its huge side effects. In this talk we will discuss why finalizers are not always a good choice, its overhead on Garbage Collector and how it can be avoided. Using simple JMH benchmark I will demonstrate the performance overhead of finalizers.

We will also talk about my firsthand experience with finalizers where just by removing finalizers I was able to fix major performance issue in a webapp for a large retailer.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Quem viu também gostou (18)

Semelhante a Finalizers –The not so Good, the Bad and the Ugly (20)

Anúncio

Mais recentes (20)

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

  1. 1. Finalizers –The not so Good, the Bad and the Ugly A Garbage Collection Saga Gautam Singh www.livemykarma.com @singhkgautam
  2. 2. Fun Day 
  3. 3. Work Day 
  4. 4. Analyzing Thread Dump
  5. 5. 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
  6. 6. Finalizable Object life Cycle
  7. 7. 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
  8. 8. Problem Class
  9. 9. Load Test Result (Before)
  10. 10. Load Test Result (After)
  11. 11. Problem with Finalizers Finalizers are unpredictable, often dangerous, and generally unnecessary Effective Java – Joshua Bloch
  12. 12. Dead Object Resurrection(Bad)
  13. 13. Dead Object Resurrection(Bad)
  14. 14. Increase Allocation Cost(Bad) Allocation(Bytes) Per Instance Around 3X
  15. 15. Cause more GCs(Ugly)
  16. 16. No Guarantee, when called (Ugly)
  17. 17. No Guarantee (Example)
  18. 18. Make object concurrent(Ugly)
  19. 19. 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
  20. 20. How to avoid Finalizers Pitfalls
  21. 21. Do you really need finalizer?
  22. 22. Utilize Lifecycle methods, if available
  23. 23. Provide explicit termination method.
  24. 24. Reduce data overhead(Before)
  25. 25. Reduce data overhead(After)
  26. 26. Utilize Cache or Object pool Image: http://www.birdsandblooms.com/birding/birding-basics/bird-behavior-caching-food/
  27. 27. References • Effective Java (2nd Edition) By Joshua Bloch https://www.amazon.com/Effective-Java-2nd-Joshua- Bloch/dp/0321356683 • The Garbage Collection Handbook: The Art of Automatic Memory Management By Richard Jones, Antony Hosking, Eliot Moss http://gchandbook.org/ • Benchmark Source https://github.com/LiveMyKarma/Finalizer
  28. 28. 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
  29. 29. Questions?

×