Big Picture
Two sections.
-
The components that are created for each thread
Threads JVM System Threads Per Thread program Counter (PC) Stack Native Stack Stack Restrictions Frame Local Variables Array Operand Stack Dynamic Linking
-
The components that are created independently of threads.
Shared Between Threads Heap Memory Management Non-Heap Memory Just In Time (JIT) Compilation Method Area Class File Structure Classloader Faster Class Loading Where Is The Method Area Classloader Reference Run Time Constant Pool Exception Table Symbol Table Interned Strings (String Table)
Part I Threads
- Thread:
A thread is a thread of execution in a program. The JVM allows an application to have multiple threads of excution running concurrently.There is a direct mapping between a Java Thread and a native operating system Thread in the Hotspot JVM.
A java/native thread includes:
1. thread-local storage
2. allocation buffers [TLAB: Thread Local Allocation Buffers]
3. synchronization objects
4. stacks
5. the program counter
The main background system threads in the Hotspot JVM
1. VM thread
2. Periodic task thread
3. GC threads
4. Compiler threads
5. Signal dispatcher thread
- Per Thread:
Each thread of execution has the following components:
1. Program Counter (PC)
2. Stack
3. Native Stack
4. Stack restrictions
If a thread requires a larger stack than allowed a StackOverflowError is thrown. If a thread requires a new frame and there isn’t enough memory to allocate it then an OutOfMemoryError is thrown.
5. Frame
Each frame contains:
(1) Local variable array
boolean, byte, char, short, int, float, double, reference, returnAddress (a single slot, 32-bit)
long, double (take two consecutive slots, 64-bit)
(2) Return value
(3) Operand stack
pushing, popping, duplicating, swapping, or executing operations
(4) Reference to runtime constant pool for class of the current method
Each frame contains a reference to the runtime constant pool.
The reference points to the constant pool for the class of the method being executed for that frame.
Part II Shared Bwtween Threads
- Heap:
The Heap is used to allocate class instances and arrays at runtime. Arrays and objects can never be stored on the stack because a frame is not designed to change in size after it has been created. they are not removed when a method ends. nstead objects are only removed by the garbage collector.
To support garbage collection the heap is divided into three sections:
1. Young Generation
Often split between Eden and Survivor
2. Old Generation (also called Tenured Generation)
3. Permanent Generation
- Memory Management
Objects and Arrays are never explicitly de-allocated instead the garbage collector automatically reclaims them.
Reclaim Rules:
1. New objects and arrays are created into the young generation
2. Minor garbage collection will operate in the young generation. Objects, that are still alive, will be moved from the eden space to the survivor space.
3. Major garbage collection, which typically causes the application threads to pause, will move objects between generations. Objects, that are still alive, will be moved from the young generation to the old (tenured) generation.
4. The permanent generation is collected every time the old generation is collected. They are both collected when either becomes full.
- Non-Heap Memory:
Objects that are logically considered as part of the JVM mechanics are not created on the Heap.
1. Code Cache used for compilation and storage of methods that have been compiled to native code by the JIT compiler
2. Permanent Generation that contains
- the method area
- interned strings
-
Just In Time (JIT) Compilation
-
Java byte code is interpreted however this is NOT as fast as directly executing native code on the JVM’s host CPU.
-
To improve performance the Oracle Hotspot VM looks for “hot” areas of byte code that are executed regularly and compiles these to native code.
-
The native code is then stored in the code cache in non-heap memory.
In this way the Hotspot VM tries to choose the most appropriate way to TRADE-OFF the extra time it takes to compile code verses the extra time it take to execute interpreted code.
-
-
Method Area
- Classloader Reference
- Run Time Constant Pool
- Numeric constants
- Field references
- Method References
- Attributes
- Field data - Per field
- Name
- Type
- Modifiers
- Attributes
- Method data - Per method
- Name
- Return Type
- Parameter Types (in order)
- Modifiers
- Attributes
- Method code - Per method
- Bytecodes
- Operand stack size
- Local variable size
- Local variable table
- Exception table - Per exception handler
- Start point
- End point
- PC offset for handler code
- Constant pool index for exception class being caught
Thread safe: All threads share the same method area, so access to the method area data and the process of dynamic linking must be thread safe.
- Class File Structure
A typical compiled class file structure:
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info contant_pool[constant_pool_count – 1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
Attr | Description |
---|---|
magic, minor_version, major_version | specifies information about the version of the class and the version of the JDK this class was compiled for. |
constant_pool | similar to a symbol table although it contains more data |
access_flags | provides the list of modifiers for this class. |
this_class | index into the constant_pool providing the fully qualified name of this class i.e. org/jamesdbloom/foo/Bar |
super_class | index into the constant_pool providing a symbolic reference to the super class i.e. java/lang/Object |
interfaces | array of indexes into the constant_pool providing a symbolic references to all interfaces that have been implemented. |
fields | array of indexes into the constant_pool giving a complete description of each field. |
methods | array of indexes into the constant_pool giving a complete description of each method signature, if the method is not abstract or native then the bytecode is also present. |
attributes | array of different value that provide additional information about the class including any annotations with RetentionPolicy.CLASS or RetentionPolicy.RUNTIME |