JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential –...

36

Transcript of JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential –...

Page 1: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing
Page 2: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

JVM Memory Model and GC

Developer Community Support

Fairoz Matte Principle Member Of Technical Staff Java Platform Sustaining Engineering,

Oracle Confidential – Internal/Restricted/Highly Restricted Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Page 3: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Agenda

Oracle Confidential – Internal/Restricted/Highly Restricted 3

What is JVM?

JVM Architecture

Hotspot Memory Structure

Garbage Collection Process

Demo Application

Garbage collection Types

1

2

3

5

4

6

Page 4: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

What is JVM?

Oracle Confidential – Internal/Restricted/Highly Restricted 4

The Java Virtual Machine (JVM) is an abstract computing machine.

This way, Java programs are written to the same set of interfaces and libraries.

Each JVM implementation for a specific operating system, translates the Java programming instructions into instructions and commands that run on the local operating system. Java programs achieve platform independence.

The Java virtual machine knows nothing of the Java programming language, only of a particular binary format, the class file format.

Page 5: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

JVM Architecture

Oracle Confidential – Internal/Restricted/Highly Restricted 5

Page 6: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Hotspot Memory Structure

Oracle Confidential – Internal/Restricted/Highly Restricted 6

NEW GEN (Eden + Survivor)

OLD GEN

PERM GEN (-XX:MAXPERMSIZE)

Native Memory

JVM Process

HEAP -XMx

Pre Java 8

NEW GEN (Eden + Survivor)

OLD GEN

METASPACE (-XX:METASPACESIZE)

Native Memory

JVM Process

HEAP -XMx

Java 8

Page 7: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Hotspot Memory Structure - Terminologies

Oracle Confidential – Internal/Restricted/Highly Restricted 7

The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.

Page 8: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Hotspot Memory Structure - Terminologies

Oracle Confidential – Internal/Restricted/Highly Restricted 8

The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.

Major garbage collection are also Stop the World events. Often a major collection is much slower because it involves all live objects

Metaspace : A native memory to store class meta-data information and that grows automatically & garbage collected.

Native Memory: Native memory is the memory which is available to a process, e.g. the Java process. Native memory is controlled by the operating system and based on physical memory

Page 9: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 9

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.

Step 1: Marking

Live Object Unreferenced Object

Before Marking

After Marking

Page 10: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 10

Step 2: Normal Deletion

After Normal deletion

Memory allocated holds a list of references to free spaces, and searches for free space whenever an allocation is required

Page 11: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 11

Step 2a: Deletion with Compaction

After Normal deletion with Compaction

Memory allocated holds the reference to the beginning of free space, and allocated memory sequentially

Page 12: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 12

Heap is separated into different generations

More and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time

However, empirical analysis of applications has shown that most objects are short lived

Page 13: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 13

Object Allocation

First, any new objects are allocated to the eden space. Both survivor spaces start out empty.

Eden

Just allocated Created in Eden

S0 survivor space S1 survivor space

1 2

Page 14: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 14

Filling the eden space

When the eden space fills up, a minor garbage collection is triggered.

Eden

Just allocated Created in Eden

S0 survivor space S1 survivor space

1 2

Page 15: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 15

Copying referenced objects

Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared..

Eden

S0 survivor space S1 survivor space

1 1 1 1

Unreferenced Object

Referenced Object

Page 16: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 16

Object Aging

Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. ..

Eden

From survivor space To survivor space

1 1

Unreferenced Object

Referenced Object

2 2 1 1 1 1

Page 17: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 17

Additional Aging

At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.

Eden

To survivor space From survivor space

3 3

Unreferenced Object

Referenced Object

2 2 1 1 2 1

Page 18: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 18

Promotion

This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.

Eden

To survivor space From survivor space

Unreferenced Object

Referenced Object

8 8 1 2 3 1

Old generation

9 9

Page 19: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 19

Promotion

As minor GCs continue to occure objects will continue to be promoted to the old generation space.

Yong

Generation

Old Generation

Allocation

Promotion

Page 20: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 20

GC Process Summary

Old generation

9 9

Eden

Just allocated Created in Eden

S0 survivor space S1 survivor space

1 2

Page 21: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Process

Oracle Confidential – Internal/Restricted/Highly Restricted 21

Demo

You have seen the garbage collection process using a series of pictures. Now it is time to experience and explore the process live. In this activity, you will run a Java application and analyze the garbage collection process using Visual VM. The Visual VM program is included with the JDK and allows developers to monitor various aspects of a running JVM.

Page 22: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 22

Start a Demo Application

With the Java JDK and demos installed, you can now run demo application that will be analyzed in this activity. For this example, the demos are installed in D:\2015\jdk-8u101-windows-x64-demos

The demo application is a 2D graphics demo. To execute it type: java -Xmx12m -Xms3m -Xmn1m XX:+UseSerialGC –jar D:\2015\jdk-8u101-windows-x64-demos\jdk1.8.0_101\demo\jfc\Java2D\Java2demo.jar

Page 23: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 23

Java 2D Demo

Page 24: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 24

Start Transforms

Page 25: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 25

Start VisualVM

Page 26: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 26

Start Java2D demo application to visualize GC activities

Page 27: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Garbage Collection – Demo Application

Oracle Confidential – Internal/Restricted/Highly Restricted 27

Visual GC

Page 28: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 28

Java Garbage Collectors

You now know the basics of garbage collection and have observed the garbage collector in action on a sample application. In this section, you will learn about the garbage collectors available for Java and the command line switches you need to select them.

Switch Description

-Xms Sets the initial heap size for when the JVM starts.

-Xmx Sets the maximum heap size.

Page 29: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 29

Java Garbage Collectors – Serial GC

The serial collector is the default for client style machines in Java SE 5 and 6. With the serial collector, both minor and major garbage collections are done serially

Usage cases

The Serial GC is the garbage collector of choice for most applications that do not have low pause time requirements and run on client-style machines.

.

Page 30: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 30

Java Garbage Collectors – Serial GC

Command Line Options:

To enable the Serial Collector use: -XX:+UseSerialGC

java -Xmx12m -Xms3m -Xmn1m -XX:+UseSerialGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

.

Page 31: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 31

Java Garbage Collectors – Parallel GC (throughput collector)

The parallel garbage collector uses multiple threads to perform the young genertion garbage collection. By default on a host with N CPUs, the parallel garbage collector uses N garbage collector threads in the collection.

Usage cases

It can use multiple CPUs to speed up application throughput. This collector should be used when a lot of work need to be done and long pauses are acceptable. For example, batch processing like printing reports or bills or performing a large number of database queries.

Page 32: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 32

Java Garbage Collectors – Parallel GC

Command Line Options:

To enable the Parallel Collector use: -XX:+UseParallelGC

The number of garbage collector threads can be controlled with command-line options: -XX:ParallelGCThreads=<desired number>

Page 33: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Type

Oracle Confidential – Internal/Restricted/Highly Restricted 33

Java Garbage Collectors – CMS GC (Concurrent Mark Sweep)

It collects the tenured generation. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.

Usage cases

The CMS collector should be used for applications that require low pause times and can share resources with the garbage collector. Examples include desktop UI application that respond to events.

Page 34: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 34

Java Garbage Collectors – CMS GC

Command Line Options:

To enable the CMS Collector use: -XX:+UseConcMarkSweepGC

and to set the number of threads use: -XX:ParallelCMSThreads=<n>.

Page 35: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Generational Garbage Collection Types

Oracle Confidential – Internal/Restricted/Highly Restricted 35

Java Garbage Collectors – G1 GC (Generation 1)

The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector that has quite a different layout from the other garbage collectors described previously

Command Line Options:

To enable the G1 Collector use:

To enable the G1 Collector use: -XX:+UseG1GC

Page 36: JVM Memory Model and GC - files.meetup.com · What is JVM? Oracle Confidential – Internal/Restricted/Highly Restricted 4 The Java Virtual Machine (JVM) is an abstract computing

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Thanks

Oracle Confidential – Internal/Restricted/Highly Restricted 36