Containers for Java: Optimizing your applications · Containers for Java: Optimizing your...

Preview:

Citation preview

Containers for Java: Optimizing your applications—Mofe SalamiDeveloper Advocate

Containers are great!

Physical Server

Host Operating System

Container Engine

Bins/Libs Bins/Libs Bins/Libs

App 1 App 2 App 3

Physical Server

Host Operating System

Hypervisor

Guest OS Guest OS Guest OS

Bins/Libs Bins/Libs Bins/Libs

App 1 App 2 App 3

2

Java and containers, not so great

Java is nice but:

• Large application size• Long start-up times• Not ideal for FaaS

3

How can we address these issues?

Takeaways:

1. How to reduce application size2. Hotspot features that improve start-up times3. Other optimizations

4

What this talk will not cover

5

Important, but not in scope:

• Security• JVM vs. resource limits• Running multiple JVMs

1. How to reduce application size

6

Defining “application size”

7

• Containers are isolated environments• Dependencies are packaged with the application• Java code depends on a JVM

Application size = Linux distribution + JVM + Application Code

Choosing the “right” JDK distribution

8

Points for consideration:

• Vendor• Support and Updates• Support periods• Commercial vs. “Free”• TCK’d binaries

Read more:

Life Beyond Java 8 – Trisha GeePanel: Java Is Still Free?

OracleOpenJDKBuild

Release date Free updates superseded / ended (by Oracle)

8 (LTS) March 2014

At least through January 2020 (Personal desktop use) Commercial use ended in January 2019

9 Sept 2017 Superseded by Oracle OpenJDK build 10

10 March 2018 Superseded by Oracle OpenJDK build 11 in Sept 2018

11 (LTS) Sept 2018

To be superseded by Oracle OpenJDK build 12 in March 2019 (may be extended)

12 March 2019 To be superseded by Oracle OpenJDK build 13

13 Sept 2019 To be superseded by Oracle OpenJDK build 14

Choosing AdoptOpenJDK

9

• Free as in beer and as in speech• Built from OpenJDK source repo• Backporting patches are community-led• Maintained by the OpenJDK community• Docker images:

• latest (Full JDK)• slim (Stripped down JDK)• alpine (Full JDK)• alpine-slim (Stripped down JDK)

AdoptOpenJDK Build

Release date

Update/Support period

8 (LTS) March 2014 At least September 2023

9 Sept 2017 -

10 March 2018 -

11 (LTS) Sept 2018 At least September 2022

Choosing the “right” Linux distribution

10

docker pull adoptopenjdk/${VERSION}:${VARIANT}

Building the application

11

Simple Spring Boot Application

Dockerfile

Building the application

12

docker build -t java-containers:adoptopenjdk-${VERSION}:${VARIANT} Dockerfile

Demo #1

13

• Verify the start of java application:

docker run -p 8080:8080 --rm java-containers:adoptopenjdk-8-alpine-slim

• Verify we can reach the /ping endpoint

Go to: localhost:8080/ping

2. HotSpot features that improve start-up times

14

JVM Start-up times

15

Total start-up time = JVM start-up time + application start-up time

• What JDK will you use?• What type of application are you running?

Let’s experiment with:

• AdoptOpenJDK 11• Our sample rest application

Class Data Sharing (CDS)

16

• Oracle JVM feature• Reduces the start-up time• Creates a cache of Java Class Data• Does not factor in classes within custom JARs• Available since JDK 5

Generate cache with:• java -Xshare:dump

Use with:• -Xshare:on

Classes from system jar file

Private internal representation

Dump to shared archive file

Benchmarking our application

17

• Test start-up times on average:

• Add config to exit application after starting up:

Benchmark Results: CDS (Off vs. On)

18

Application Class Data Sharing (AppCDS)

19

• CDS for external libraries• Enables pre-processing of classes to be loaded• Speeds up start-up time • Was commercial only until JDK 10

Steps to use:

• Identify a list of classes to cache• Generate the cache• Run application with AppCDS cache

AppCDS: Generating shared cache file

20

• Create classes.lst which stores list of classes to cache:

• Create application cache from classes.lst at app-cds.jsa

AppCDS: Run application with AppCDS cache

21

• Run application with AppCDS cache:

• Note the cache size:

Benchmark Results: CDS vs AppCDS

22

Ahead-of-time Compilation (AOT)

23

• Compile java classes to native code• Skips JVM JIT compilation at start-up time • Restricted to Linux x64 systems running 64-bit Java • Available since JDK 9

Steps to use:

• Identify a list of classes to cache• Generate the the AOT cache• Run application with AOT cache

AOT: Generating shared cache file

24

• Reuse classes.lst from AppCDS use case

• Create AOT cache from classes.lst at lib.so

AOT: Run application with CDS + AppCDS + AOT

25

• Run application with AOT cache:

• Note the cache size:

Benchmark Results: CDS vs CDS + AppCDS + AOT

26

Things to note

27

• Down to experimentation• Optimisations depend on application mark-up• AOT Cache size trade-off• Bigger caches can increase start-up times!• Exclude some classes from AOT cache• Classpath on launch and cache must match• More benefits for multiple JVMs using caches• Cache is OS-specific!

But wait… this is also a container talk…

28

• Let’s make a new Dockerfile:

• Java 11 for features• Use alpine-slim to reduce image size• Copy in the caches• We add the appropriate flags

But wait… this is also a container talk…

29

• Let’s build the image:

docker build -t java-container-opt:adoptopenjdk-11-alpine-slim .

• Check the output:

3. Other optimizations

30

An alternative JVM: OpenJ9

31

• Contributed by IBM to the Eclipse Foundation in 2017• Boasts “Low memory footprint” & “Fast start-up time”• CDS and AOT implementations also included• Builds by AdoptOpenJDK available for Java 8+

Stephen Hellberg – Open J9: Compelling Java for Cloud Workloads

OpenJ9: Putting it to the test (AdoptOpenJDK 11)

32

OpenJ9: Putting it to the test (AdoptOpenJDK 8)

33

They have images for it too!

34

Demo #2

35

AdoptOpenJDK 11 (CDS, AppCDs, AOT)

Vs.

AdoptOpenJDK 8 OpenJ9 (CDS, AOT)

Honorable mention 1: Graal and Subtrate VM

36

• Takes AOT to a new level• Compiles Java code into self-contained executables• Does not run on the Java HotSpot VM• Runs in SubstrateVM in the executable• Boasts faster start-up times & lower runtime memory overhead

Duncan McGregor - Graal: Not Just a New JIT for the JVM

Honorable mention 2: Using modules & jLink

37

• Java modules and jLink introduced in JDK9• jLink identifies the modules required in the JRE• Strip the JRE of unused classes• Reduce the application size and improve performance• Requires coding efforts to embrace modules

Try it out!Workshop: https://github.com/IBMCodeLondon/java-containers101

Our London Meetup: https://www.meetup.com/IBM-Code-London

38

39

Recommended