- Survivor 1 + Survivor 2
- Old (or Tenured)
- Metaspace (Since Java 8) / Perm Generation (until Java 7)
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:
- 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
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
JVM natively supports multithreading running on multiple cores.
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.
- 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
-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
-Xms: set initial heap size
-Xmx: set maximum heap size
-Xss: set thread stack size
e.g. set max RAM to 31 GB:
-verbose:gc: logs garbage collector info
-Xlog:gc: more logs on garbage collectors.
-XX:+HeapDumpOnOutOfMemoryError: trigger heap dump on out of memory
Choose Garbage Collector
-ea: Enable assertion on all application classes. By default,
assertstatements in your are not execution to save execution time.
-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.
GCLogoptions are now handled by