JVM Memory Management

1,682 views
1,446 views

Published on

Java VM Memory Management and Garbage Collection Tuning

Published in: Software
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,682
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
64
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

JVM Memory Management

  1. 1. Java Virtual Machine (JVM) Memory Management & Monitoring
  2. 2. Java Performance • Application Algorithm Inefficiencies • In-process / Multi-Threads Inter-locking • I/O Bound • Memory Management (Garbage Collection)
  3. 3. Memory Management
  4. 4. JVM Heap Memory • Different Version JVM, Different Default Heap Size • Initial Heap = Physical Memory / 64 • 4GB Physical = 64MB • Max Heap = Physical Memory / 4 • 4GB Physical = 1GB • Can be Override by -Xms and -Xmx
  5. 5. -Xms 1.5g -Xmx 3g
  6. 6. -Xms 1.5g -Xmx 3g
  7. 7. -Xms 1.5g -Xmx 3g
  8. 8. Out of Memory (OOM)
 Outcomes • Extremely Slow JVM or Not Responding • High CPU Load • Memory/Disk Swapping • A lot of Available Memory • JVM Crashed
  9. 9. Garbage Collection
  10. 10. Memory Pools (Not G1GC)
  11. 11. Memory Pools • Different Version JVM = Different Default Size Memory Pools • Sizing by Ratio, Not Absolute Number • Different Garbage Collector = Different Memory Pools
  12. 12. Garbage Collection • All new allocations in Eden (Super Fast) • When Eden has no space • Stop the World • Copy Collect the data into Survivor • After several collection, Survivor will promote to Old Gen • Old Gen Fragmentation
  13. 13. Garbage Collectors • Young Gen Collectors • -XX:+UseSerialGC • -XX:+UseParallelGC (Java 6 Default, GC in other Thread) • -XX:+UseParallelOldGC • -XX:+UseParNewGC • -XX:+UseG1GC (Java 7 Experimental)
  14. 14. Garbage Collectors • Old Gen Collectors • MarkSweepCompact (enabled with -XX:UseSerialGC) • PS MarkSweep (enabled with -XX:UseParallelOldGC) • -XX:ConcMarkSweepGC (Default Java 7) • G1 Mixed Generation (enabled with -XX:+UseG1GC)
  15. 15. Garbage Collectors Combinations • 2 Garbage Collections for 2 Types of Garbage Collections • Young Gen => Survivor • Survivor => Old Gen • See http://www.fasterj.com/articles/oraclecollectors1.shtml
  16. 16. Throughput Collectors • Long Pause • Can Tune Automatically • -XX:+UseAdaptiveSizePolicy • -XX:MaxGCPauseMillis=… • -XX:GCTimeRatio=…
  17. 17. Low Pause Collectors • Use these if Application response time is sensitive • Works best with multiple CPU cores (4 cores min. for Java apps) • 2 Cores for JVM + Garbage Collectors • 2+ Cores for Application • -XX:+UseConcMarkSweepGC • Best Garbage Collectors for Web Services • -XX:+UseG1GC • Auto Memory Pools Size Adjustment • Very Fast, No Old Gen Fragmentation
  18. 18. GC Tuning • Do not touch anything unless you know what you are doing! • Default settings for Modern JVM is usually very good • Common Problems • Mix Bag of Multiple Garbage Collectors Configurations • Different JVM Version Different Default Settings • Old Settings have No Meaning Anymore
  19. 19. Examples
  20. 20. Example Service -Xms256M -Xmx1536M ! -XX:NewSize=128m -XX:MaxNewSize=512m ! -XX:+CMSPermGenSweepingEnabled ! -XX:PermSize=128m -XX:MaxPermSize=256m ! -XX:+CMSClassUnloadingEnabled ! -XX:+UseConcMarkSweepGC ! -XX:+HeapDumpOnOutOfMemoryError
  21. 21. Tips • For Java server application, -Xms should be the same as -Xms • Give JVM Heap as much as possible • 4GB or below has the best performance • 4GB+ may need JVM Heap Tuning • Bigger the Young Gen • Less Minor GC => More Full GC
  22. 22. Tips • Bigger the Young Gen • Less Minor GC => More Full GC • Most Concurrent GC (other than G1) • -XX:+UseConcMarkSweepGC -XX:+UseParNewGC • -XX:+UseParallelGC should not be used with -XX: +UseConcMarkSweepGC
  23. 23. Best JVM Heap Configuration
  24. 24. NONE
  25. 25. Example -Xms2048m -Xmx2048m ! -XX:PermSize=512m -XX:MaxPermSize=512m! -XX:+UseConcMarkSweepGC -XX:+UseParNewGC! -XX:MaxGCPauseMillis=2000! -XX:+CMSPermGenSweepingEnabled ! -XX:+CMSClassUnloadingEnabled ! -XX:+UseCompressedOops! -XX:+HeapDumpOnOutOfMemoryError! -XX:+PrintGCTimeStamps ! -XX:+PrintGCApplicationStoppedTime ! -XX:+PrintGCApplicationConcurrentTime! !
  26. 26. JVM Tuning • Don’t adjust memory pool size unless • Too many Full GC • GC Takes Too long • GC does not have high yield • Adjust NewRatio, NewSize, MaxNewSize based on performance testing • Considering using G1 instead of CMS

×