DevNexus 2018 – Garbage Collection in Java 9

Title: Garbage Collection in Java 9
Speakers: Chandra Guntur

For more blog posts, see the DevNexus 2018 live blogging table of contents


Areas of memory

  • By thread
  • Method area
  • Runtime constant pool
  • Heap

Definitions

  • Types of collectors – serial (one thread; stopping all app threads), parallel (multiple threads, but still stops all app threads), concurrent (doesn’t stop app)
  • Can mark objects precisely or conservatively
  • Can run all at once or incrementally
  • Can move reachable objects or just release unreachable objects

Patterns

  • reference counting
    • simplest algorithm
    • keeps a counter for each object
    • obsolete for commercial JVMs
  • mark/sweep/compact
    • change marker type if used
    • remove if not used
    • compact – like Windows defragment
  • copying
    • similar to mark/sweep/compact except copies object when used instead of marking
    • the copying operating compacts/defragments since copied to adjacent memories
  • Java 5 introduced generational GC
    • start young
    • most objects go away rather than getting promoted to older generation
    • generations – eden, survivor, tenured
  • Java 5 – 8 had Concurrent Mark-Sweep (CMS)
    • serial collection and tenured generation
    • most popular GC in Java 5-8
    • deprecated in Java 9

Problems

  • inconsistent pause type
  • poor performance for large memory heaps
  • focused more on live objects than garbage

Java 9 – G1GC (Garbage First Garbage Collector)

  • new default in Java 9
  • region – contiguous unit of memory
  • heap divided into 2048+ regions of equal size – may be 1-32MB each
  • each region has a generation (free, eden, survivor, tenured) or be part of a “humongous” allocation
  • humongous objects
    • use complete (or multiple contiguous) regions
    • must be the size 50% of a region or more.
  • goal of  consistent pause time
  • life of a region
    • objects get created and stored in “young” half
    • at some point, does initial mark where concurrent marking process determines which objects are alive. young collection continues in parallel.
    • then does re-mark to confirm still alive. young collection still continues
    • decides if should reclaim space now or wait
    • then does space reclamation
  • young collection includes survivor promotion and tenured promotion
  • java -XX:+PrintFlagsInitial -version > output – print the initial defaults to a file since it is a long list – always same at startup by OS
  • java -XX:+PrintFlagsFinal -version > output – print the initial defaults to a file since it is a long list – changes based on actual running
  • there were lots of involved details. hard to have that as the last session of the day :).

My take

I’m not a fan of “ignore the text; read it later” presentations. That is what the appendix or notes view is for. I’d rather have the pictures and key points be bigger. Chandra even zoomed in on the diagram. It cold have had a whole slide to itself. That said, the presentation was clear and easy to follow.

DevNexus 2018 – Modules in Action

Title: Modules in Action
Speakers: Mark Reinhold

For more blog posts, see the DevNexus 2018 live blogging table of contents


Demo used Java 10 RC.

Using JShell to look at modules

  • “foo”.getClass().getModule() – module java.base
  • “foo”.getClass().getModule().getClass() – class java.lang.Module
  • new java.sql.Timestamp(0).getModule() – module java.sql

Modules

  • java –list-modules – ex: java.sql@10
  • There are 75 modules in Open JDK 10

JavaDoc

  • The Java 9 JavaDoc shows modules on the home page instead of all the packages.
  • Clicking a module shows you the packages in the module.
  • Java 9 JavaDoc has search box

Random facts

  • tree – unix commands that list what is in directory and children (not installed by default)
  • Showed that can use * in classpath to get all files in lib directory. (since Java 6)

Creating a bare bones module

  • Create module-info.java file in root of src folder
  • Simplest module file is one line: module moduleName { }
  • If run javap against the class file, you can see “requires java.base;” was added. This is like a constructor calling super(). It is mandatory so you don’t need to type it.
  • jar –file x.jar –describe-module – shows name of file, requires and the package contained
  • java –module-path lib -m moduleName/className – run the program
  • java -classpath jar className – can still run a modular jar on the classpath
  • jar –create –file x.jar –main-class class -C classes – create a jar that can run without specifying the class to run
  • java –modulepath lib -m moduleName – run the program without specifying it by name

Refactoring the module to split into two

  • module module1 { requires packageName; }
  • module module2 { exports packageName; }

Problems

  • If remove module2 and try to run, get java.lang.module.FindException on startup since can’t find module. Error inlcudes “Error occurred during initialization of boot layer”
  • If forgot to type “requires”, and compile get “package x is not visible”
  • If two modules require each other and compile, get “cyclic dependence involving package x”
  • If forget to type “exports” and compile, get “IllegalAccessError exception” along with long message about exporting

jlink

  • Optional Java linker
  • New directory jmods in JAVA_HOME.
  • Each module in the JDK has a file in jmods
  • JDK got a lot bigger with Java 9 because shipping two copies of each module – the main one and the jmods directory
  • jlink –module-path $JAVA_HOME.jmods –output jre –add-modules java.base – creates a JRE that is an order of magnitude smaller than the full one (44M vs 329B)
  • Can include your custom app into the image in addition to JDK
  • jlink –module-path $JAVA_HOME.jmods:lib –output jrePlusApp –add-modules java.base myCustomModule
  • jlink has a –compress flag to make file smaller
  • Not platform independent. Can cross link to create file for other operating system

Reflection

  • Want to make it so private really means private and final really means final. Vs reflection today.
  • Illegal reflective access – first time, get warning. The warning tells you how to enable more warnings. There probably are, but don’t want to blast you with them. Eventually, warning will turn into a hard error.
  • Not planning to disallow illegal reflective access until after Java 11.
  • Supplementing “exports”, is “open. It allows extra reflection

Inside a jmod

  • jmods are like a zip file with extra structure.
  • classes directory with all of the classes
  • conf directory for files in runtime image
  • include directory for jni header files
  • legal directory with licenses if any
  • bin directory for launchers
  • lib directory for native code

My take

I like the live demo approach along with the emphasis on both concepts and common problems. I also like that Mark left a lot of time for Q&A.

 

DevNexus 2018 – Gradle – the basics and beyond

Title: Gradle – the Basics and Beyond
Speakers: Ken Kousen

For more blog posts, see the DevNexus 2018 live blogging table of contents


Install

  • requires Java 7+
  • Gradle 4.2.1+ supports Java 9
  • Gradle releases frequently but is very backward compatible
  • Just unzip and add to path
  • The complete distribute includes the samples. The sample sources are also available online.

General

  • Gradle is now much faster than Maven
  • gradle.com is the enterprise version which is built on top of open source gradle.org.
  • Gradle is mostly implemented in Java. Groovy is for the DSL.
  • Uses Ant behind the scenes as well
  • Groovy comes with Gradle. Don’t need to install Groovy separately
  • gretty is jetty for gradle; also contains tomcat

Docs

Future

  • Kotlin
    • Creating a Kotlin DSL for simple build files.
    • Kotlin is not replacing Groovy
    • Not yet at 1.0.0
    • Goal is better IDE integration
  • Generator
    • Looking at being able to generate projects on web project like Spring Boot has

gradlew

  • Recommend using gradlew. Checking in the file guarantees users are on version of gradle that you intended
  • Has effect of creating many gradle installs on your machine. Not that big.

Creating a project

  • Can generate new project or port basic pom files.
  • Use porting as a guide and then hand edit
  • Generate new project in current directory (make empty directory by hand first)
    • mkdir proj
    • cd proj
    • gradle init –type java-application (or java-library)
  • Files/directories in project
    • gradlew/gradlew.bat – wrapper script
    • gradle/wrapper – jar and properties
    • settings.gradle – lists multi project builds
    • gradle.properties – key/value pairs used by build
  • In home directory’x .gradle
  • wrapper/distx – the binarry/installs.

Daemon

  • Improves performance
  • Cache a lot of the JVM startup
  • For a few hours, same daemon used if using same version of gradle on every project.
  • Tip: turn off daemon on CI servers so every build is a clean build

Gradle file

  • repositories
    • can use jcenter or maven central.
    • or can specify a different one for any Maven or Ivy repo; like internal corporate one
    • can list multiple repos and searches in order
  • dependencies
    • ‘group:name:version’ – same concept as Maven GAV. Or can split into three sections. Ken likes the former better; either fine.
    • can write compile (‘g:n:v’) to specify fully. Groovy parens are optional except when they are not. The DSL can interpret without parens and easier to read.
    • New Java plugin has api/implementation to use as scope instead of compile.
    • compile files – lets you add files not in the classpath
    • compile fileTree – lets you add a directory not in the classpath
  • plugins
    • if registered on plugins.gradle.org, can list id and version numbers.
    • built in plugins (ex: java) don’t get a version number

Commands

  • gradle build -t – rebuilds and then sits idle until you change code. Like a rebuild loop
  • gradle build –dry-run – shows what would run (can use -m instead of –dry-run)
  • gradle -b… – use different build file name
  • gradle –stsatus – shows processes

Build scan

  • Starting Gradle 4.3, will prompt you if you haven’t accepted the license agreement and try to use.
  • It’s hosted at Gradle so sends some info out.
  • Gradle Enterprise does build scans in house

My take

Good structured intro/review. Happy that Ken changed the background to white when we were reading code. He also made the code sufficiently large. I didn’t even need my glasses. Go Ken! The only time I needed my classes was when he showed the manual which uses dark gray on a light gray background for the comments. I knew more of the basics than I realized. But good to have it gel better in my head! And I definitely learned things. Rushed at the end though and that was where most of the new stuff is. Glad I’m a New Yorker and can handle talking fast. Can’t type that fast though so stopped taking notes.