containers for java optimizing your applications
play

Containers for Java: Optimizing your applications Mofe Salami - PowerPoint PPT Presentation

Containers for Java: Optimizing your applications Mofe Salami Developer Advocate Containers are great! App 1 App 2 App 3 Bins/Libs Bins/Libs Bins/Libs App 1 App 2 App 3 Guest OS Guest OS Guest OS Bins/Libs Bins/Libs Bins/Libs


  1. Containers for Java: Optimizing your applications — Mofe Salami Developer Advocate

  2. Containers are great! App 1 App 2 App 3 Bins/Libs Bins/Libs Bins/Libs App 1 App 2 App 3 Guest OS Guest OS Guest OS Bins/Libs Bins/Libs Bins/Libs Hypervisor Container Engine Host Operating System Host Operating System Physical Server Physical Server 2

  3. Java and containers, not so great Java is nice but: • Large application size • Long start-up times • Not ideal for FaaS 3

  4. How can we address these issues? Takeaways: 1. How to reduce application size 2. Hotspot features that improve start-up times 3. Other optimizations 4

  5. What this talk will not cover Important, but not in scope: • Security • JVM vs. resource limits • Running multiple JVMs 5

  6. 1. How to reduce application size 6

  7. Defining “application size” • Containers are isolated environments • Dependencies are packaged with the application • Java code depends on a JVM Application size = Linux distribution + JVM + Application Code 7

  8. Choosing the “right” JDK distribution Oracle Points for consideration: Release Free updates superseded / ended (by Oracle) OpenJDK date Build • Vendor March At least through January 2020 (Personal desktop use) 8 (LTS) • 2014 Commercial use ended in January 2019 Support and Updates • Sept Support periods 9 Superseded by Oracle OpenJDK build 10 2017 • Commercial vs. “Free” March • 10 Superseded by Oracle OpenJDK build 11 in Sept 2018 TCK’d binaries 2018 Sept To be superseded by Oracle OpenJDK build 12 in 11 (LTS) 2018 March 2019 (may be extended) Read more: March 12 To be superseded by Oracle OpenJDK build 13 2019 Life Beyond Java 8 – Trisha Gee Panel: Java Is Still Free? Sept 13 To be superseded by Oracle OpenJDK build 14 2019 8

  9. Choosing AdoptOpenJDK • Free as in beer and as in speech Adopt • Built from OpenJDK source repo Release Update/Support OpenJDK date period • Backporting patches are community-led Build • Maintained by the OpenJDK community At least 8 (LTS) March 2014 • Docker images: September 2023 9 Sept 2017 - • latest (Full JDK) 10 March 2018 - • slim (Stripped down JDK) • alpine (Full JDK) At least 11 (LTS) Sept 2018 September 2022 • alpine-slim (Stripped down JDK) 9

  10. Choosing the “right” Linux distribution docker pull adoptopenjdk/${VERSION}:${VARIANT} 10

  11. Building the application Simple Spring Boot Application Dockerfile 11

  12. Building the application docker build -t java-containers:adoptopenjdk-${VERSION}:${VARIANT} Dockerfile 12

  13. Demo #1 • 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 13

  14. 2. HotSpot features that improve start-up times 14

  15. JVM Start-up times 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 15

  16. Class Data Sharing (CDS) Classes from • Oracle JVM feature system jar file • Reduces the start-up time • Creates a cache of Java Class Data • Does not factor in classes within custom JARs • Available since JDK 5 Private internal representation Generate cache with: • java -Xshare:dump Dump to shared Use with: archive file • -Xshare:on 16

  17. Benchmarking our application • Test start-up times on average: • Add config to exit application after starting up: 17

  18. Benchmark Results: CDS (Off vs. On) 18

  19. Application Class Data Sharing (AppCDS) • 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 19

  20. AppCDS: Generating shared cache file • Create classes.lst which stores list of classes to cache: • Create application cache from classes.lst at app-cds.jsa 20

  21. AppCDS: Run application with AppCDS cache • Run application with AppCDS cache: • Note the cache size: 21

  22. Benchmark Results: CDS vs AppCDS 22

  23. Ahead-of-time Compilation (AOT) • 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 23

  24. AOT: Generating shared cache file • Reuse classes.lst from AppCDS use case • Create AOT cache from classes.lst at lib.so 24

  25. AOT: Run application with CDS + AppCDS + AOT • Run application with AOT cache: • Note the cache size: 25

  26. Benchmark Results: CDS vs CDS + AppCDS + AOT 26

  27. Things to note • 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! 27

  28. But wait… this is also a container talk… • 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 28

  29. But wait… this is also a container talk… • Let’s build the image: docker build -t java-container-opt:adoptopenjdk-11-alpine-slim . • Check the output: 29

  30. 3. Other optimizations 30

  31. An alternative JVM: OpenJ9 • 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 31

  32. OpenJ9: Putting it to the test (AdoptOpenJDK 11) 32

  33. OpenJ9: Putting it to the test (AdoptOpenJDK 8) 33

  34. They have images for it too! 34

  35. Demo #2 AdoptOpenJDK 11 (CDS, AppCDs, AOT) Vs. AdoptOpenJDK 8 OpenJ9 (CDS, AOT) 35

  36. Honorable mention 1: Graal and Subtrate VM • 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 36

  37. Honorable mention 2: Using modules & jLink • 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 37

  38. Try it out! Workshop: https://github.com/IBMCodeLondon/java-containers101 Our London Meetup: https://www.meetup.com/IBM-Code-London 38

  39. 39

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend