Successfully reported this slideshow.

Understanding JVM

3,654 views

Published on

Introduction to JVM memory structure and reasons for different OutOfMemoryError.

Published in: Technology

Understanding JVM

  1. 1. Understanding JVM Aparna Chaudhary Friday, October 4, 13
  2. 2. VM Memory What’s inside my VM? Friday, October 4, 13
  3. 3. VM Memory Guest OS Memory What’s inside my VM? Friday, October 4, 13
  4. 4. JVM Memory VM Memory Guest OS Memory What’s inside my VM? Friday, October 4, 13
  5. 5. JVM Memory VM Memory ize ermS XX:MaxP - PermGen Guest OS Memory What’s inside my VM? Friday, October 4, 13
  6. 6. JVM Memory VM Memory read S Per Th -XS ize ermS XX:MaxP - Stack PermGen Guest OS Memory What’s inside my VM? Friday, October 4, 13
  7. 7. HEAP JVM Memory VM Memory read S Per Th -XS ize ermS XX:MaxP - Stack PermGen Guest OS Memory What’s inside my VM? Friday, October 4, 13
  8. 8. HEAP Initial -Xms JVM Memory VM Memory read S Per Th -XS ize ermS XX:MaxP - Stack PermGen Guest OS Memory What’s inside my VM? Friday, October 4, 13
  9. 9. -Xmx HEAP Initial -Xms JVM Memory VM Memory read S Per Th -XS ize ermS XX:MaxP - Stack PermGen Guest OS Memory What’s inside my VM? Friday, October 4, 13
  10. 10. OutOfMemoryError Exception in thread "main": java.lang.OutOfMemoryError: Java heap space Exception in thread "main": java.lang.OutOfMemoryError: PermGen space Exception in thread "main": java.lang.OutOfMemoryError: GC overhead limit exceeded Exception in thread "main": java.lang.OutOfMemoryError: Unable to create new native thread and more... Friday, October 4, 13
  11. 11. OutOfMemoryError Exception in thread "main": java.lang.OutOfMemoryError: PermGen Space Friday, October 4, 13
  12. 12. Perm Gen Space Used for Storing the JVM’s internal representation of Java Classes Friday, October 4, 13
  13. 13. Object described by references Class references loaded by Classl oader Life of a class Friday, October 4, 13
  14. 14. Object described by references Class references loaded by references Classl oader Life of a class Friday, October 4, 13
  15. 15. references Object references Object Class Class references references Classloader1 Classloader2 Life of a class Friday, October 4, 13
  16. 16. references Object references Object Class Class references references Class Classloader1 Class Class Class Class Class Class Class Classloader2 Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Life of a class Friday, October 4, 13
  17. 17. references Object references Object Class Class references references Class Classloader1 Class Class Class Class Class Class Class Classloader2 Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Class Life of a class Friday, October 4, 13
  18. 18. OOM - PermGen This occurs when JVM wants to load new class definitions; but there is not enough space in PermGen space Friday, October 4, 13
  19. 19. OOM - PermGen Symptom At server startup Cause Bigger PermGen footprint Resolution Increase PermGen capacity -XX:MaxPermSize Friday, October 4, 13
  20. 20. OOM - PermGen Symptom PermGen space increases gradually Cause Possible classloader leak Resolution Investigate any third party API you are using for any potential class loader leak defect Friday, October 4, 13
  21. 21. OOM - PermGen Symptom OOM after deploy-redeploy cycles Cause Possible classloader leak in the App Server Resolution Someone should fix it for you. Hmm..where is EAP? Friday, October 4, 13
  22. 22. OutOfMemoryError Exception in thread "main": java.lang.OutOfMemoryError: GC overhead limit exceeded Friday, October 4, 13
  23. 23. OOM - GC overhead limit exceeded Policy to allow the VM to detect potential OutOfMemoryError conditions earlier and before it runs out of Java Heap space. Can be turned off -XX:-UseGCOverheadLimit But don’t! Friday, October 4, 13
  24. 24. OOM - GC overhead limit exceeded When is this error triggered? ✓Too ✓Too Friday, October 4, 13 many Full GC iterations much time spent in GC
  25. 25. OutOfMemoryError Exception in thread "main": java.lang.OutOfMemoryError: Unable to create new native thread Friday, October 4, 13
  26. 26. OOM - Unable to create new native thread JVM is asking a new thread from the OS and the underlying OS cannot allocate a new thread anymore. Friday, October 4, 13
  27. 27. Why? -Xmx HEAP Initial -Xms JVM Memory d Stack ize VM Memory PermGen Threa -XSS Per ermS -XX:MaxP Guest OS Memory Friday, October 4, 13 not eno ugh stack space
  28. 28. OOM - Unable to create new native thread ✓ Check ulimit - process limit ✓ Consider using thread-pools ✓ Reduce heap space or perform Friday, October 4, 13 vertical scaling
  29. 29. OutOfMemoryError Exception in thread "main": java.lang.OutOfMemoryError: Java heap space Friday, October 4, 13
  30. 30. JVM Heap Structure Survivor Space Eden S0 S1 Young Generation Friday, October 4, 13 Tenured Old Generation
  31. 31. Object Allocation Eden From To Survivor Spaces Young Gen = Eden + Survivor Spaces Friday, October 4, 13
  32. 32. Object Allocation are allocated in Eden Just allocated Eden From To Survivor Spaces Young Gen = Eden + Survivor Spaces Friday, October 4, 13
  33. 33. Filling up Eden Space Eden From To Survivor Spaces Friday, October 4, 13
  34. 34. Filling up Eden Space are allocated in Eden Just allocated Eden From To Survivor Spaces Friday, October 4, 13
  35. 35. Minor GC - Copy referenced objects Eden From To Survivor Spaces Unreferenced Referenced Friday, October 4, 13
  36. 36. Minor GC - Copy referenced objects Eden From To Survivor Spaces Unreferenced Referenced Friday, October 4, 13
  37. 37. Minor GC - Copy referenced objects Eden From To Survivor Spaces Unreferenced Referenced Friday, October 4, 13
  38. 38. Minor GC - Copy referenced objects Eden To 1 1 1 1 Survivor Spaces From Unreferenced Referenced Friday, October 4, 13
  39. 39. Next Minor GC - Object Aging Eden From 1 1 1 1 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  40. 40. Next Minor GC - Object Aging Eden From 1 1 1 1 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  41. 41. Next Minor GC - Object Aging Eden From 1 1 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  42. 42. Next Minor GC - Object Aging Eden From 1 2 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  43. 43. Next Minor GC - Object Aging Eden From 2 2 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  44. 44. Next Minor GC - Object Aging Eden From 2 2 1 Survivor Spaces To Unreferenced Referenced Friday, October 4, 13
  45. 45. Next Minor GC - Object Aging Eden From 2 2 1 Survivor Spaces 1 To Unreferenced Referenced Friday, October 4, 13
  46. 46. Next Minor GC - Additional Aging Eden To 2 2 1 Survivor Spaces 1 From Unreferenced Referenced Friday, October 4, 13
  47. 47. Next Minor GC - Additional Aging Eden To 2 1 Survivor Spaces From Unreferenced Referenced Friday, October 4, 13
  48. 48. Next Minor GC - Additional Aging Eden To 1 2 3 Survivor Spaces From Unreferenced Referenced Friday, October 4, 13
  49. 49. Next Minor GC - Promotion Eden From 1 14 1 2 3 To Survivor Spaces Tenured Unreferenced Referenced Friday, October 4, 13
  50. 50. Next Minor GC - Promotion Eden From 1 1 2 3 To Survivor Spaces Tenured 14 Unreferenced Referenced Friday, October 4, 13
  51. 51. Major GC Eden From 1 1 1 2 3 To Survivor Spaces Tenured Unreferenced Referenced Friday, October 4, 13
  52. 52. Major GC Eden From 1 1 1 2 3 To Survivor Spaces Tenured Unreferenced Referenced Friday, October 4, 13
  53. 53. Warning! This knowledge does not make us JVM experts! Always focus first on software before hacking JVM. Friday, October 4, 13
  54. 54. JVM Diagnosis jmap jhat Friday, October 4, 13
  55. 55. Class Histogram jmap -F -histo `pgrep java` Make it a practice to proactively use this command after a load test and endurance test. Friday, October 4, 13
  56. 56. Heap Dump jmap [ option ] <pid> jmap -F -dump:live,format=b,file=/ var/opt/jboss-as/tmp/io/ heapdump.hprof `pgrep java` http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html Friday, October 4, 13
  57. 57. What Next? Demo VisualVM Eclipse Memory Analyzer Tool Other tools and commands GC Algorithms (Optional) Friday, October 4, 13

×