Understanding JVM

57
Understanding JVM Aparna Chaudhary Friday, October 4, 13

description

Introduction to JVM memory structure and reasons for different OutOfMemoryError.

Transcript of Understanding JVM

Page 1: Understanding JVM

Understanding JVMAparna Chaudhary

Friday, October 4, 13

Page 2: Understanding JVM

What’s inside my VM?

VM Memory

VM Memory

Friday, October 4, 13

Page 3: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

VM Memory

Guest OS Memory

Friday, October 4, 13

Page 4: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

VM Memory

Guest OS Memory

JVM Memory

Friday, October 4, 13

Page 5: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPerm

Size

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPerm

Size

Friday, October 4, 13

Page 6: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per ThreadVM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

Friday, October 4, 13

Page 7: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Friday, October 4, 13

Page 8: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Initial-Xms

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Initial-Xms

Friday, October 4, 13

Page 9: Understanding JVM

What’s inside my VM?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Initial-Xms

-Xmx

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Initial-Xms

-Xmx

Friday, October 4, 13

Page 10: Understanding JVM

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

OutOfMemoryError

Friday, October 4, 13

Page 11: Understanding JVM

Exception in thread "main": java.lang.OutOfMemoryError: PermGen Space

OutOfMemoryError

Friday, October 4, 13

Page 12: Understanding JVM

Perm Gen Space

Used for Storing the JVM’s internal representation of Java Classes

Friday, October 4, 13

Page 13: Understanding JVM

Life of a class

described by

loaded by

references

references

Object

Class

Classloader

described by

loaded by

references

references

Object

Class

Classloader

Friday, October 4, 13

Page 14: Understanding JVM

Life of a class

described by

loaded by

references

references

Object

Class

Classloader

references

described by

loaded by

references

references

Object

Class

Classloader

references

Friday, October 4, 13

Page 15: Understanding JVM

Life of a class

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Friday, October 4, 13

Page 16: Understanding JVM

Life of a class

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Friday, October 4, 13

Page 17: Understanding JVM

Life of a class

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Object

Class

Classloader1

references

referencesObject

Class

Classloader2

references

references

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Class

Friday, October 4, 13

Page 18: Understanding JVM

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

Page 19: Understanding JVM

OOM - PermGen

SymptomAt server startup

CauseBigger PermGen footprint

ResolutionIncrease PermGen capacity -XX:MaxPermSize

Friday, October 4, 13

Page 20: Understanding JVM

OOM - PermGen

SymptomPermGen space increases gradually

CausePossible classloader leak

ResolutionInvestigate any third party API you are using for any potential class loader leak defect

Friday, October 4, 13

Page 21: Understanding JVM

OOM - PermGen

SymptomOOM after deploy-redeploy cycles

CausePossible classloader leak in the App Server

ResolutionSomeone should fix it for you. Hmm..where is EAP?

Friday, October 4, 13

Page 22: Understanding JVM

OutOfMemoryError

Exception in thread "main": java.lang.OutOfMemoryError: GC overhead limit exceeded

Friday, October 4, 13

Page 23: Understanding JVM

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

Page 24: Understanding JVM

OOM - GC overhead limit exceeded

When is this error triggered?

✓Too many Full GC iterations✓Too much time spent in GC

Friday, October 4, 13

Page 25: Understanding JVM

Exception in thread "main": java.lang.OutOfMemoryError: Unable to create new native thread

OutOfMemoryError

Friday, October 4, 13

Page 26: Understanding JVM

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

Page 27: Understanding JVM

Why?

VM Memory

Guest OS Memory

JVM Memory

PermGen-XX:MaxPermSize

Stack-XSS Per Thread

HEAP

Initial-Xms

-Xmx

not enough stack space

Friday, October 4, 13

Page 28: Understanding JVM

OOM - Unable to create new native thread

✓Check ulimit - process limit✓Consider using thread-pools✓Reduce heap space or perform vertical scaling

Friday, October 4, 13

Page 29: Understanding JVM

Exception in thread "main": java.lang.OutOfMemoryError: Java heap space

OutOfMemoryError

Friday, October 4, 13

Page 30: Understanding JVM

JVM Heap Structure

Eden S0 S1 Tenured

Old Generation

YoungGeneration

SurvivorSpace

Friday, October 4, 13

Page 31: Understanding JVM

Eden

To From

Survivor Spaces

Object Allocation

Young Gen = Eden + Survivor SpacesFriday, October 4, 13

Page 32: Understanding JVM

Eden

To From

Survivor Spaces

Just allocatedare allocated in Eden

Object Allocation

Young Gen = Eden + Survivor SpacesFriday, October 4, 13

Page 33: Understanding JVM

Eden

To From

Survivor Spaces

Filling up Eden Space

Friday, October 4, 13

Page 34: Understanding JVM

Eden

To From

Survivor Spaces

Just allocatedare allocated in Eden

Filling up Eden Space

Friday, October 4, 13

Page 35: Understanding JVM

Minor GC - Copy referenced objects

Eden

To From

Survivor Spaces Unreferenced

Referenced

Friday, October 4, 13

Page 36: Understanding JVM

Minor GC - Copy referenced objects

Eden

To From

Survivor Spaces Unreferenced

Referenced

Friday, October 4, 13

Page 37: Understanding JVM

Minor GC - Copy referenced objects

Eden

To From

Survivor Spaces Unreferenced

Referenced

Friday, October 4, 13

Page 38: Understanding JVM

Minor GC - Copy referenced objects

Eden

To From

Survivor Spaces Unreferenced

Referenced

1 1 1 1

Friday, October 4, 13

Page 39: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

1 1 1 1

Friday, October 4, 13

Page 40: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

1 1 1 1

Friday, October 4, 13

Page 41: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

1 1

Friday, October 4, 13

Page 42: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

1 2

Friday, October 4, 13

Page 43: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

2 2

Friday, October 4, 13

Page 44: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

2 2 1

Friday, October 4, 13

Page 45: Understanding JVM

Next Minor GC - Object Aging

Eden

From To

Survivor Spaces Unreferenced

Referenced

2 2 1 1

Friday, October 4, 13

Page 46: Understanding JVM

Next Minor GC - Additional Aging

Eden

To From

Survivor Spaces Unreferenced

Referenced

2 2 1 1

Friday, October 4, 13

Page 47: Understanding JVM

Next Minor GC - Additional Aging

Eden

To From

Survivor Spaces Unreferenced

Referenced

2 1

Friday, October 4, 13

Page 48: Understanding JVM

Next Minor GC - Additional Aging

Eden

To From

Survivor Spaces Unreferenced

Referenced

321

Friday, October 4, 13

Page 49: Understanding JVM

Next Minor GC - Promotion

Eden

From To

Survivor Spaces

Unreferenced

Referenced

3211 14

Tenured

Friday, October 4, 13

Page 50: Understanding JVM

Next Minor GC - Promotion

Eden

From To

Survivor Spaces

Unreferenced

Referenced

3211

14Tenured

Friday, October 4, 13

Page 51: Understanding JVM

Major GC

Eden

From To

Survivor Spaces

Unreferenced

Referenced

3211 1

Tenured

Friday, October 4, 13

Page 52: Understanding JVM

Major GC

Eden

From To

Survivor Spaces

Unreferenced

Referenced

3211 1

Tenured

Friday, October 4, 13

Page 53: Understanding JVM

Warning!

This knowledge does not make us JVM experts!

Always focus first on software before hacking JVM.

Friday, October 4, 13

Page 54: Understanding JVM

JVM Diagnosis

jmap

jhat

Friday, October 4, 13

Page 55: Understanding JVM

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

Page 56: Understanding JVM

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

Page 57: Understanding JVM

What Next?

Demo

VisualVM

Eclipse Memory Analyzer Tool

Other tools and commands

GC Algorithms (Optional)

Friday, October 4, 13