JVM Memory Management
description
Transcript of JVM Memory Management
Java Virtual Machine (JVM)
Memory Management & Monitoring
Java Performance
• Application Algorithm Inefficiencies
• In-process / Multi-Threads Inter-locking
• I/O Bound
• Memory Management (Garbage Collection)
Memory Management
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
-Xms 1.5g -Xmx 3g
-Xms 1.5g -Xmx 3g
-Xms 1.5g -Xmx 3g
Out of Memory (OOM)Outcomes
• Extremely Slow JVM or Not Responding
• High CPU Load
• Memory/Disk Swapping
• A lot of Available Memory
• JVM Crashed
Garbage Collection
Memory Pools(Not G1GC)
Memory Pools
• Different Version JVM = Different Default Size Memory Pools
• Sizing by Ratio, Not Absolute Number
• Different Garbage Collector = Different Memory Pools
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
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)
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)
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
Throughput Collectors
• Long Pause
• Can Tune Automatically
• -XX:+UseAdaptiveSizePolicy
• -XX:MaxGCPauseMillis=…
• -XX:GCTimeRatio=…
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
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
Examples
Example Service
-Xms256M -Xmx1536M !-XX:NewSize=128m -XX:MaxNewSize=512m !-XX:+CMSPermGenSweepingEnabled !-XX:PermSize=128m -XX:MaxPermSize=256m !-XX:+CMSClassUnloadingEnabled !-XX:+UseConcMarkSweepGC !-XX:+HeapDumpOnOutOfMemoryError
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
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
Best JVM Heap Configuration
NONE
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!!
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