logo

JVM

Last Updated: 2022-04-08

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 native platform executable binaries (small, fast, require less CPU and memory) 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

JVM Options

  • - standard options, expected to be accepted by all JVM implementations and are stable between releases
  • -X: eXtensions, non-standard options, not guaranteed to be supported on all JVM implementations, subject to change without notice in subsequent releases of the Java SDK.
  • -XX: developer options, not recommended for casual use. These options are also subject to change without notice.
  • -D: define system properties. Use this value by System.getProperty. the definition is in the context of the application not in the Java interpreter context

Heap Size

  • -Xms: set initial heap size
  • -Xmx: set maximum heap size
  • -Xss: set thread stack size

e.g. set max RAM to 31 GB: -Xmx31g.

Debug

  • -verbose:gc: logs garbage collector info
  • -Xlog:gc: more logs on garbage collectors.
  • -XX:+HeapDumpOnOutOfMemoryError: trigger heap dump on out of memory

Profile

  • -Xprof
  • -Xrunhprof

Choose Garbage Collector

  • -XX:+UseG1GC
  • -XX:+UseShenandoahGC

Others

  • -ea: Enable assertion on all application classes. By default, assert statements in your are not execution to save execution time.

Deprecated Options

  • -XX:PermSize: PermGen is replaced by Metaspace in Java 8.
  • -XX:+UseConcMarkSweepGC: CMS collector was deprecated in JDK 9 and was removed in JDK 14.
  • GCLog options are now handled by Xlog