Java8 bench gc

Post on 18-Jul-2015

97 views 2 download

Tags:

Transcript of Java8 bench gc

Intro to functional programming.

Hidetomo Morimotohttp://github.com/mocchidesu

2015/02/20

Brief history of Java Lamdas / Stream API

Why “functional” language? Benchmark multi-core

Basic garbage collection Open JDK / HotSpot VM? G1GC

James Gosling: Father of Java

Understand this hell

java –jar -XX:+UseG1GC-XX:MaxGCPauseMillis=200-Xx:InitiatingHeapOccupancyPercent=45-XX:NewRatio=n (young generation)-XX:SurvivorRatio=n (eden/survivor space)-XX:ParallelGCThreads=n-XX:+ScavengeBeforeFullGC-XX:G1HeapRegionSize=8M(UseParallelGC/UseSerialGC/UseConcMarkSweepGC)

Blah Blah Blah…

Date Version Highlights

1991 Oak by James Gosling@Sun Microsystems

Named after oak tree stood outside his office. For small PDA device. Slight flavor of C

1996 Jan 23 JDK 1.0 “Oak” sounds similar to “awk”?

1997 Java 1.1 JDBC, reflection, JIT compiler

1998 Java2 (J2SE) Collection framework, Swing(GUI) API

2000 Java 1.3 JNDI, RMI

2002 Feb. Java 1.4 Regex, JAXP

2004 Sept. Java 1.5 Generics, annotation, enum, concurrency, varargs

2006 Dec. Java 1.6 Performance tune, JDBC4

2011 July Java 1.7 JVM support dynamic language (syntax wise, no change)

FeaturesDateTime API (no more new Date(), Calendar.getInstance()!)Nashborn JavaScript engine ($JAVA_HOME/bin/jjs)Default method in Interface / Improved concurrent package

Lambda expressions@FunctionalInterface annotationsMethod Referencejava.util.Stream package

Backward CompatibilityBinary compatibility && Behavioral compatibility - ExcellentSource compatibility - Good

Lambda is a syntax “sugar” of anonymous class that has 1 method: Functional Interface. (Runnable, Comparable, etc.)

@Abstract Word Clod for Concurrencyhttp://www.123rf.com/photo_16617606_abstract-word-cloud-for-concurrency-with-related-tags-and-terms.html

Task Parallelism vs. Data Parallelism. Functional Language approach Same Idea: MapReduce Google BigTable

(utilize thousands of commodity machines)

Functional LanguageReferential Transparency

Return is purely determined by input (no Side Effect)

Always the same return Easier to Memonized

Function is the 1st class object

Sequence of elements from a source that supports aggregate operations(Out of 250M people. Pick age 20-30, Living within X miles from LA, male, etc.)

Start with .stream(), chain with intermediate operators, end with terminal operator.Intermediate Operator

(filter, map, sorted, distinct, limit)Terminal Operator (forEach, collect, reduce, iterator)

Developer likes benchmark, hates meeting. Microbenchmarking framework. JMH

Numerous reasons you don’t do it yourself.▪ HotSpot VM compiles “hotspot”▪ VM optimize code flow

Annotation basis Open JDK

https://github.com/mocchidesu/java8bench

Let’s Talk about OLD Technology

Since 1960. Not changed for 50 years. Only 3 basic algorithms.

Mark & Sweep GC by John McCarthy(1960) Copy GC by Marvin Minsky (1963) Reference Counting GC by George E. Collins

(1960)

Mark Phase: Flag all the accessible object. (from root, register, call stack) Sweep Phase: Remove unreferenced.

Pros Simple / Easy

Cons Memory Fragmentation Stop the world (Entire heap

scan)

Heap divided into two equal space (To : From)

Pros No Memory

Fragmentation Cons

Overhead of copying. Requires twice the size.

Increment / Decrement reference count each object.

Pros Quick GC

Cons Circular Reference

Circular Reference

Any of recent GC is the mix of those 3. Generational GC is an approach. ▪ Young generation▪ Survivor space (copy gc)▪ Old / Tenured

90%+ objects are gone immediatelyWhy do we need to process full GC each time?

Let’s divided them up.

If Object survived through GC process one time, promote to Survivor SpaceIf Object survived XX times, promote to Tenured space.Young generation GC (Scavenge) happens more often than Tenured space.

Garbage First Garbage Collection Generational High Throughput Optimized for Server / Client architecture Memory compaction Introduced Java1.7 ver…

java –jar -XX:+UseG1GC (Generational, high throughput, suitable for server-client architecture)

-XX:MaxGCPauseMillis=200-Xx:InitiatingHeapOccupancyPercent=45-XX:NewRatio=n (young generation)-XX:SurvivorRatio=n (eden/survivor space)-XX:ParallelGCThreads=n (number of cores)-XX:+ScavengeBeforeFullGC(UseParallelGC/UseSerialGC/UseConcMarkSweepGC)

Lambda is not only a syntax sugar It’s a new era of Java When use? NOW GC is fun

Oracle Java Garbage Collectionhttp://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Oracle Compatibility Guide for JDK 8http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html

Lambda Peek under the Hood by Brian Goetz - Java One 2013http://www.slideshare.net/jaxlondon2012/lambda-a-peek-under-the-hood-brian-goetz10 Example of Lambda Expressions and Streams in Java 8http://javarevisited.blogspot.com/2014/02/10-example-of-lambda-expressions-in-java8.html