[BGOUG] Java GC - Friend or Foe

41
Java Garbage Collector: Friend or Foe Krasimir Semerdzhiev Development Architect / SAP Labs Bulgaria

description

Introduction to Java Grabage Collection, presented at Bulgarian Oracle User Group event, Nov 2011

Transcript of [BGOUG] Java GC - Friend or Foe

Page 1: [BGOUG] Java GC - Friend or Foe

Java Garbage Collector:

Friend or Foe

Krasimir SemerdzhievDevelopment Architect / SAP Labs Bulgaria

Page 2: [BGOUG] Java GC - Friend or Foe

Agenda

1. Brief historical view

2. Myths and Urban legends

3. GC machinery

4. Try to stay out of trouble

Page 3: [BGOUG] Java GC - Friend or Foe

History of Garbage Collection

A long time ago, in a galaxy far far away…

* Counting from [McCarthy 1959]

McCarthy (1959)

LISt Processor (LISP)

Reference counting (IBM)

Naive Mark/sweep GC

Semi-space collector

1959*

Knuth (1973/1978)

Copying collector

Mark/sweep GC

Mark/don’t sweep GC

1970 1990 2000 2011

Appel (1988), Baker (1992)

Generational GC

Train GC

Stop the world

Cheng-Blelloch (2001)

Concurrent

Parallel

Real-Time GC

2003

Bacon, Cheng, Rajan (2003)

Metronome GC

Garbage first GC (G1)

Ergonomics

20091995

Page 4: [BGOUG] Java GC - Friend or Foe

Agenda

1. Brief historical view

2. Myths and Urban legends

3. GC machinery

4. Try to stay out of trouble

5. Tools for the masses

Page 5: [BGOUG] Java GC - Friend or Foe

Myths

Java and C/C++ performance

Is C/C++ faster than Java?

The short answer: it depends.

/Cliff Click

Page 6: [BGOUG] Java GC - Friend or Foe

Myths

My GC cleans up tons of memory – what’s going on.

String s = "c"?

= ref + 8 + (12 + 2) + 4 + 4 + 4 > 34 bytes.

/** The value is used for character storage. */

private final char value[];

/** The offset is the first index of the storage that is used. */

private final int offset;

/** The count is the number of characters in the String. */

private final int count;

/** Cache the hash code for the string */

private int hash; // Default to 0

/** use serialVersionUID from JDK 1.0.2 for interoperability */

private static final long serialVersionUID =

-6849794470754667710L;

Page 7: [BGOUG] Java GC - Friend or Foe

Is Java bad at memory management?

public static void main(String[] args) throws Exception {

final long start = Runtime.getRuntime().freeMemory();

final byte[][] arrays = new byte[100][];

for (int i = 0; i < arrays.length; i++) {

arrays[i] = new byte[100];

long current = Runtime.getRuntime().freeMemory();

System.out.println(start + " " + current);

Thread.sleep(1000);

}

}

Page 8: [BGOUG] Java GC - Friend or Foe

Agenda

1. Brief historical view

2. Myths and Urban legends

3. GC machinery*

4. Try to stay out of trouble

5. Tony Printezis

* Credits for the GC insights go to

Tony Printezis and his excellent

J1 sessions

Page 9: [BGOUG] Java GC - Friend or Foe

Garbage collector

Let’s start with a simple memory example…

Page 10: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Sweep

Root object

Mark-Sweep

Page 11: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Sweep

Root object

Mark-Sweep – Marking…

Page 12: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Sweep

Root object

Free List Root

Mark-Sweep – Sweeping…

Page 13: [BGOUG] Java GC - Friend or Foe

Garbage collector

Let’s try another one …

Page 14: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Compact

Root object

Mark-Compact

Page 15: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Compact

Root object

Mark-Compact – Marking…

Page 16: [BGOUG] Java GC - Friend or Foe

Garbage collector

Mark-Compact

Root object

Mark-Sweep – Compacting…

Free Pointer

Page 17: [BGOUG] Java GC - Friend or Foe

Garbage collector

Let’s try another one …

Page 18: [BGOUG] Java GC - Friend or Foe

Garbage collector

Copying

Root object

Copying

From space

To space Free and unused

Page 19: [BGOUG] Java GC - Friend or Foe

Garbage collector

Copying

Root object

Copying – Evacuation

From space

To space Free and unused

Page 20: [BGOUG] Java GC - Friend or Foe

Garbage collector

Copying

Root object

Copying – Flipping

From space

To space Free and unused

Free Pointer

Page 21: [BGOUG] Java GC - Friend or Foe

Garbage collector

Let’s try another one … ;o)

Page 22: [BGOUG] Java GC - Friend or Foe

Garbage collector

Generational Garbage Collection

Generational Garbage Collection – moving to more modern times

Young generation

Old Generation

Allocations

Promotion

Page 23: [BGOUG] Java GC - Friend or Foe

Java Heap

Hotspot JVM

Memory layout

Hotspot JVM (Java) heap layout

Young generation

Old Generation

Perm Generation

Everything

else

Page 24: [BGOUG] Java GC - Friend or Foe

Java Heap

Hotspot JVM

Memory layout

Hotspot JVM (Java) heap layout

Young generation

Old Generation

Perm Generation

Everything

else

Maximum size is limited:

■ 32 bit -> 2Gb

■ 64 bit -> way larger

If 2Gb is the max for the process – you can’t get it all

for the Java heap only!

Page 25: [BGOUG] Java GC - Friend or Foe

Hotspot JVM

Memory layout

Hotspot JVM (Java) heap layout

Survivor spaces

Old Generation

Perm Generation

Young generation

unused

Eden

From To

Page 26: [BGOUG] Java GC - Friend or Foe

Hotspot JVM

Memory layout

Hotspot JVM (Java) – (Small) GC running

Survivor spaces

Old Generation

Perm Generation

Young generation

unused

Eden

From To

Page 27: [BGOUG] Java GC - Friend or Foe

Hotspot JVM

Memory layout

Hotspot JVM (Java) – (Minor) GC running

Survivor spaces

Old Generation

Perm Generation

Young generation

unused

Eden

From To

Page 28: [BGOUG] Java GC - Friend or Foe

Hotspot JVM

Memory details

-Xmx, -Xms, -Xmn

Control the Java Object heap size only

Doesn’t have impact on Perm size, native heap and the Thread stack size

-XX:PermSize, -XX:MaxPermSize

Stores class definitions, methods, statis fields

Common reason for OOM errors.

-Xss

Configures the stack size of every thread.

-XX:+UseTLAB, -XX:-UseTLAB, -XX:+PrintTLAB

Enables the Thread Local Allocation Buffer.

Since Java SE 1.5 – this is automatically tuned to each and every thread.

TCP Connection buffer sizes – allocated in native space

Use Socket.setSendBufferSize(int) and Socket.setReceiveBufferSize(int).

OS will use the smaller of the two or will simply ignore that setting.

Object allocation statistics:

■ Up to 98% of new objects are

short-lived

■ Up to 98% die before another

Mb is allocated

Page 29: [BGOUG] Java GC - Friend or Foe

Agenda

1. Brief historical view

2. Myths and Urban legends

3. GC machinery

4. Try to stay out of trouble

Page 30: [BGOUG] Java GC - Friend or Foe

OutOfMemoryError

How to proceed?

java.lang.OutOfMemoryError: PermGen space

Increase the Perm Space – will help if there is no code generation happening

In case of a leak – the only solution is frequent system restart.

java.lang.OutOfMemoryError: unable to create new native thread

Decrease –Xmx or –Xss

java.lang.StackOverflowError

Increase –Xss or fix the corresponding algorithm

IOException: Too many open files (for example)

Increase the OS file handle limit per process

Check for leaking file handles

Physical limit of the VM is 2048 ZIP/JAR files opened at the same time.

java.lang.OutOfMemoryError: Direct buffer memory

Direct mapped memory – used for java.nio.ByteBuffer

Increase -XX:MaxDirectMemorySize

Page 31: [BGOUG] Java GC - Friend or Foe

Finalizer methods

Good or bad

Again short answer: it depends ;-)

Infrastructure components

Might be used for debugging/tracing purposes

Major scenario – closing of critical backend resources

All finalizer methods are collected separately. No mass-wipe is performed on them!

Never, ever throw an exception in a finalizer!

Applications

Avoid finalizers by all means!

Per-request created objects

Avoid finalizers by all means!

Finalizer queue

Working threadsFinalizer thread (singleton)

Page 32: [BGOUG] Java GC - Friend or Foe

Response time peaks

without CMS

All those are full GCs…

Page 33: [BGOUG] Java GC - Friend or Foe

No more full GCs…

Response time peaks

with CMS

Page 34: [BGOUG] Java GC - Friend or Foe

Garbage Collection Strategies

Does it pay off to play with that?

-XX:+UseSerialGC

Default state before Java 5. Obsolete!

-XX:+UseParallelGC - Parallel Scavange GC (1.4.2)

Works on Young Generation only

Use -XX:ParallelGCThreads to configure it

-XX:+UseParNewGC - Parallel New-Gen GC (5.0)

Use also –XX:SurvivorRatio and –XX:MaxTenuringThreshold to

define the lifespan of objects in the eden space.

-XX:+CMSClassUnloadingEnabled to trigger concurrent cleanup of

the Perm space

-XX:+UseConcMarkSweepGC - Concurrent Old-Gen GC

Default since Java SE 5.

-XX:+UseParallelOldGC - Concurrent Old-Gen GC (5.0)

Parallel Compaction of Old space

Terms:

• Serial – 1 thing at a

time

• Parallel – work is

done in multiple

threads

• Concurrent – work

happens

simultaneously

Page 35: [BGOUG] Java GC - Friend or Foe

Garbage Collection Ergonomics

What’s that?

New way to configure GC – specify:

Max pause time goal (-XX:MaxGCPauseMillis)

Throughput goal (-XX:GCTimeRatio)

Assumes minimal footprint goal

Use -XX:+UseParallelGC with those.

Garbage First (G1) GC will be the default in Java SE 7. Released

with JDK 1.6.0_u14 for non-productive usage.

Enable that for testing via:

-XX:+UnlockExperimentalVMOptions

-XX:+UseG1GC

Page 36: [BGOUG] Java GC - Friend or Foe

Analyzing Garbage Collection output

Getting GC output is critical for further analysis!

-verbose:gc get 1 line per GC run with some basic inf

-XX:+PrintGCDetails get more extended info (perm, eden, tenured)

-XX:+PrintGCTimeStamps get timestamps since the start of the VM. Allows correlation

-XX:-TraceClassUnloading get also the unloaded classes – helps tracing leaks

-Xloggc:gc.log direct GC output to a special file instead of the process output

-XX:+HeapDumpOnOutOfMemoryError Heap dump generation on OutOfMemory

Page 37: [BGOUG] Java GC - Friend or Foe

GC Viewer

GC output viewer

Import the gc.out file.

Correlate over time

So far the most

comprehensive viewer

Stay tuned and monitor

the Eclipse news ;-)

Page 38: [BGOUG] Java GC - Friend or Foe

Visual VM

Supplied by Oracle with JDK

Free and very comprehensive

Evolves together with the VM.

Focus shifting from that to

Mission Control (the Jrockit

profiling solution)

Page 39: [BGOUG] Java GC - Friend or Foe

Eclipse Memory Analyzer

Developed by SAP and IBM in Eclipse

Track GC roots

Do what-if analysis

SQL-like query language

Custom filters

Track Leaking

classloaders

Page 40: [BGOUG] Java GC - Friend or Foe

Garbage Collection – the universal settings

There are NO universal settings! Sorry. :(

G1 is the first GC, trying to go in that direction and leave the self-tuning to the VM

You have to test with realistic load!

You have to test on realistic hardware!

Tune the GC as you fix the memory leaks which will inevitably show up.

Try to find the balance of uptime/restart intervals.

Page 41: [BGOUG] Java GC - Friend or Foe

ContactQuestions?

Krasimir Semerdzhiev

[email protected]