Top Banner
Java Virtual Machine (JVM) Memory Management & Monitoring
27

JVM Memory Management

May 10, 2015

Download

Software

Andy Chan

Java VM Memory Management and Garbage Collection Tuning
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JVM Memory Management

Java Virtual Machine (JVM)

Memory Management & Monitoring

Page 2: JVM Memory Management

Java Performance

• Application Algorithm Inefficiencies

• In-process / Multi-Threads Inter-locking

• I/O Bound

• Memory Management (Garbage Collection)

Page 3: JVM Memory Management

Memory Management

Page 4: JVM 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

Page 5: JVM Memory Management

-Xms 1.5g -Xmx 3g

Page 6: JVM Memory Management

-Xms 1.5g -Xmx 3g

Page 7: JVM Memory Management

-Xms 1.5g -Xmx 3g

Page 8: JVM Memory Management

Out of Memory (OOM)Outcomes

• Extremely Slow JVM or Not Responding

• High CPU Load

• Memory/Disk Swapping

• A lot of Available Memory

• JVM Crashed

Page 9: JVM Memory Management

Garbage Collection

Page 10: JVM Memory Management
Page 11: JVM Memory Management

Memory Pools(Not G1GC)

Page 12: JVM Memory Management

Memory Pools

• Different Version JVM = Different Default Size Memory Pools

• Sizing by Ratio, Not Absolute Number

• Different Garbage Collector = Different Memory Pools

Page 13: JVM Memory Management

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

Page 14: JVM Memory Management

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)

Page 15: JVM Memory Management

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)

Page 16: JVM Memory Management

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

Page 17: JVM Memory Management

Throughput Collectors

• Long Pause

• Can Tune Automatically

• -XX:+UseAdaptiveSizePolicy

• -XX:MaxGCPauseMillis=…

• -XX:GCTimeRatio=…

Page 18: JVM Memory Management

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

Page 19: JVM Memory Management

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

Page 20: JVM Memory Management

Examples

Page 21: JVM Memory Management

Example Service

-Xms256M -Xmx1536M !-XX:NewSize=128m -XX:MaxNewSize=512m !-XX:+CMSPermGenSweepingEnabled !-XX:PermSize=128m -XX:MaxPermSize=256m !-XX:+CMSClassUnloadingEnabled !-XX:+UseConcMarkSweepGC !-XX:+HeapDumpOnOutOfMemoryError

Page 22: JVM Memory Management

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

Page 23: JVM Memory Management

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

Page 24: JVM Memory Management

Best JVM Heap Configuration

Page 25: JVM Memory Management

NONE

Page 26: JVM Memory Management

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!!

Page 27: JVM Memory Management

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