Java8 bench gc

25
Intro to functional programming. Hidetomo Morimoto http://github.com/mocchidesu 2015/02/20

Transcript of Java8 bench gc

Page 1: Java8 bench gc

Intro to functional programming.

Hidetomo Morimotohttp://github.com/mocchidesu

2015/02/20

Page 2: Java8 bench gc

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

Page 3: Java8 bench gc

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…

Page 4: Java8 bench gc

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)

Page 5: Java8 bench gc

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

Page 6: Java8 bench gc

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

Page 7: Java8 bench gc

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

Page 8: Java8 bench gc

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

(utilize thousands of commodity machines)

Page 9: Java8 bench gc

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

Page 10: Java8 bench gc

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)

Page 11: Java8 bench gc

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

Page 12: Java8 bench gc

Let’s Talk about OLD Technology

Page 13: Java8 bench gc

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)

Page 14: Java8 bench gc

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)

Page 15: Java8 bench gc

Heap divided into two equal space (To : From)

Pros No Memory

Fragmentation Cons

Overhead of copying. Requires twice the size.

Page 16: Java8 bench gc

Increment / Decrement reference count each object.

Pros Quick GC

Cons Circular Reference

Circular Reference

Page 17: Java8 bench gc

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

Page 18: Java8 bench gc

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

Page 19: Java8 bench gc

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.

Page 20: Java8 bench gc

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

Page 21: Java8 bench gc

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)

Page 22: Java8 bench gc

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

Page 23: Java8 bench gc

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

Page 24: Java8 bench gc
Page 25: Java8 bench gc