Please enable JavaScript.
Coggle requires JavaScript to display documents.
JVM (Happens-Before (Program Order Rule (code must be run in exact order…
JVM
Happens-Before
-
-
-
-
-
Thread Interruption Rule
interrupt() must happen before the thread detects interruption, like Thread.interupted(): Boolean
-
Transitivity
if A happens-before B, B happens-before C, then A happens-before C
Tools
-
VisualVM
find most-called, longest-run methods
-
-
-
-
-
-
Class Loading
-
-
create a Class object in heap for this class, as access to method area data of this class
check file format
magic number, versions, constants tags, UTF-8 encoding, etc.
-
-
-
-
-
Symbolic Reference
a series of symbols (like a pointer) to locate the target reference. target not requred to be loaded in memory yet
Direct Reference
a pointer/handle to target directly, relevant to VM implementation, target must be loaded in memory
try to load the class to be resolved, then check access, fields, methods from subclass upto superclasses until found or not
MUST be triggered when new, getstatic, putstatic, invokestatic is called
-
-
-
execute <clinit>()
<clinit>() is generated by compiler collecting
all static variable assignments and static blocks, in order given in code
<clinit>() of superclass is executed first, recursively
-
-
-
Dispatch
Static dispatch
dispatch method call based on static type of variable, which is determined during compile time
-
Virtual Method Table
stores location of each method of a class, initialised in JVM linking phase
-
if subclass overrides a method, the method entry in virtual method table of the subclass points to subclass;
Otherwise, it points to the same entry as in super class, invoking the implementation in super class
-
-
-
-
runtime memory model
Method Area
-
store class metadata, constants, static variables and JIT-compiled machine code
-
-
-
-
-
Program Counter Register
-
-
for native method, this is Undefined
Metaspace
from Java8, all class metadata, static vars, constants are stored in metaspace, off-heaply
Reference type
-
-
-
Phantom
not effecting lifetime, just to get a system notification before collected
GC
-
finalize()
if finalize() is overwritten and not executed before, put this object in F-Queue
-
finalize() is NOT ensured to complete, could be killed to avoid hanging or too slow
high cost, order of execution not ensured, could be killed,
NOT recommended to use, try-finally is better
When GC of Class?
- no instance of the class alive
- classloader of the class is collected
- corresponding java.lang.Class object has NO reference, no way to access its methods via reflection
Class
fields
-
-
-
-
-
-
-
-
-
attribute_info
code
-
-
StackMapTable
-
store what local vars, stack state should be when a code block starts
-
-
How referencing works?
Handle pool
-
when object is moved in heap, only data pointer in handle pool is changed, local variable reference remains the same
-
JIT
steps
-
-
- control flow optimisation
code reordering, splitting, removal
-
-
-
-
-
-
-
-
escape analysis
if a value is accessible outside the current code scope (return value, public variables, etc.), then it's escape. non-escape values can be optimised
:question:GC and memory allocation optimisation
on-stack allocation: allocate object on stack if its lifetime is within the call stack
-
-
-
the location of the method in code cache is recorded, so future calls will call the compiled code
at any given time, the JVM process consists of JVM executable files and a set of JIT-compiled code that is linked dynamically to the bytecode interpreter in the JVM
What is JIT?
Just-In-Time compilation which interprets java methods into native methods to gain better performance.
used only when the method has been called N times (N>threshold). call count is reset after JIT triggered
JIT has different levels of optimisation aggressiveness, applied gradually as JIT is triggered multiple times