JVM

Updated: 2021-11-19

JVM Languages:

  • Java
  • Kotlin
  • Scala
  • Groovy
  • Closure
  • Python(Jython)
  • Ruby(JRuby)

Memory

  • Eden
  • Survivor 1 + Survivor 2
  • Old (or Tenured)
  • Metaspace (Since Java 8) / Perm Generation (until Java 7)

Java Memory Model

Java Memory Model

The Java memory model describes how threads in the Java programming language interact through memory. The Java memory model defines when changes to memory made by one thread become visible to another thread

Purpose of Each Region

  • Eden Generation: newly constructed objects. Short-lived objects will soon be collected as garbage
  • Survivor: Objects that survived Minor GC are not directly promoted to the Old Generation. They are kept in the Survivor region for a certain number of Minor GC collections. Only if they survive certain number of Minor GC collections can they be promoted to the Old Generation.
  • Old Generation: long-lived objects are promoted to old generation. e.g. Application Context, HTTP Sessions, Caches, Connection Pools, etc.
  • Perm Generation: where JVM objects such as Classes, Methods, String Interns, etc. are created.
  • Metaspace: Starting with Java 8, the perm generation has been replaced with Metaspace for performance reasons.

Interpreter vs. JIT Compiler

javac compiles Java code to bytecode; to execute the bytecode, there are 2 options:

  • Interpreter:
    • Read the next byte code to be executed
    • Look at what the byte code is and find the native machine instruction(s) that correspond to it
    • Execute the native machine instruction(s)
    • Go to step 1
  • Just-In-Time compiler:
    • Read all the byte codes for the method that needs to be executed
    • Convert all those byte codes to native machine instructions
    • Execute the generated native machine instructions

invoke

  • invokestatic: static methods
  • invokevirtual: public and protected non-static methods via dynamic dispatch
  • invokeinterface: similar to invokevirtual except for the method dispatch being based on an interface type
  • invokespecial: invoke instance initialization methods(constructors) as well as private methods and methods of a superclass.
  • invokedynamic: new in Java 7, foundation of lambda and other JVM languages. https://www.javaworld.com/article/2860079/learn-java/invokedynamic-101.html

Package: java.lang.invoke

Concurrency

JVM natively supports multithreading running on multiple cores.

GraalVM

a JVM written in Java (utilizing Hotspot runtime).

  • performance: GraalVM offers advanced optimizations, such as partial escape analysis and inlining heuristics.
  • easy maintenance and incremental improvements: GraalVM compiler is written from scratch, using Java in a modular and expandable way.

Compilation:

  • GraalVM Compiler: a Java just-in-time compiler
  • GraalVM Native Image: an ahead-of-time compilation technology that produces executable binaries of class files.

Use case: Facebook used GraalVM Community as a replacement of OpenJDK, runs Spark on GraalVM: https://medium.com/graalvm/graalvm-at-facebook-af09338ac519