Performance Engineering – Related Java Concepts
-
Performance Engineering – Related Java Concepts
-
JVM Architecture – Understanding, Validating the JVM components
- Leverage the jVisualVM tool @ {JDK}/bin
- This tool will be capturing the metrics of any tool/software that internally uses
- JVM Eg: If we try to open jMeter, jVisualVM keeps track of jMeter
- This tool will be capturing the metrics of any tool/software that internally uses
- Leverage the jVisualVM tool @ {JDK}/bin
-
-
Performance Monitoring – JVM
-
JVM Architecture
- JVM acts as a run-time engine to run Java applications
- JVM converts java byte code (the generated information as a .class file once you compile a .java) into machine language code
-
- Important memory areas, individual modules and activities within a JVM
- Class Loader Subsystem
- Loading
- Modules and Activities
- Bootstrap Class Loader
- Loads the rt.jar to the classpath
- Bootstrap Class Loader
- Modules and Activities
- Loading
- Class Loader Subsystem
-
-
-
-
- Extension Class Loader
- Loads the internal .jars from /ext folder to the classpath
- Extension Class Loader
-
-
-
-
-
-
-
- Application Class Loader
- Loads the environment variables to the classpath
- Application Class Loader
-
-
-
-
-
-
- Linking
-
-
-
-
-
-
- Modules and Activities
- Verify
- Modules and Activities
-
-
-
-
-
-
-
-
-
- A Byte Code Verifier will validate if the generated byte code is proper
- Prepare
-
-
-
-
-
-
-
-
-
-
-
- Memory gets allocated and assigned with default values for static variables in source code
-
- Resolve
- The previous default value will be replaced with original value
-
- Initialization
-
-
- Runtime Data Areas
- Modules and Activities
- Method Area
- This is a shared resource (only 1 method area per JVM). All JVM threads share this same Method area, so the access to the Method data and the process of dynamic linking must be thread safe.
- Method area stores class level data(including static variables) such as:
- Class loader reference
- Run time constant pool Field data
- Method data
- Method code
- Heap Area (Shared among Threads)
- This is also a shared resource (only 1 heap area per JVM). Information of all objects and their corresponding instance variables and arrays are stored in the Heap area. Since the Method and Heap areas share memory for multiple threads, the data stored in Method & Heap areas are not thread safe. Heap area is a great target for GC.
- Stack Area (per Thread)
- This is not a shared resource. For every JVM thread, when the thread starts, a separate runtime stack gets created in order to store method calls. For every such method call, one entry will be created and added (pushed) into the top of runtime stack and such entry. It is called a Stack Frame.
- Method Area
- Modules and Activities
-
-
-
- PC Registers (per Thread)
- For each JVM thread, when the thread starts, a separate PC (Program Counter) Register gets created in order to hold the address of currently-executing instruction (memory address in the Method area). If the current method is native then the PC is undefined. Once the execution finishes, the PC register gets updated with the address of next instruction.
- Native Method Stack (per thread)
- There is a direct mapping between a Java thread and a native operating system thread. After preparing all the state for a Java thread, a separate native stack also gets created in order to store native method information (often written in C/C++) invoked through JNI (Java Native Interface).
- PC Registers (per Thread)
- Execution Engine
- The actual execution of the byte code occurs here. Execution Engine executes the instructions in the byte code line-by-line by reading the data assigned to above runtime data areas.
- Modules and Activities
- Interpreter
- The interpreter interprets the byte code and executes the instructions one-by-one. Hence, it can interpret one byte code line quickly, but executing the interpreted result is a slower task. The disadvantage is that when one method is called multiple times, each time a new interpretation and a slower execution are required.
- Interpreter
- Just-In-Time (JIT) Compiler
- If only the interpreter is available, when one method is called multiple times, each time the interpretation will also occur, which is a redundant operation if handled efficiently. This has become possible with JIT compiler. First, it compiles the entire byte code to native code (machine code). Then for repeated method calls, it directly provides the native code and the execution using native code is much faster than interpreting instructions one by one. The native code is stored in the cache, thus the compiled code can be executed quicker.
- Execution Engine qualifies to become a key subsystem when introducing performance optimizations by JVM vendors. Among such efforts, the following 4 components can largely improve its performance.
- Intermediate Code Generator produces intermediate code.
- Code Optimizer is responsible for optimizing the intermediate code generated above.
- Target Code Generator is responsible for generating Native Code (i.e. Machine Code). Profiler is a special component, responsible for finding performance bottlenecks a.k.a.
- hotspots (e.g. instances where one method is called multiple times)
- Garbage Collector (GC)
- As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory. In general, garbage collection happens under the hood, however we can trigger it by calling System.gc() method (Again the execution is not guaranteed. Hence, call Thread.sleep(1000) and wait for GC to complete).
- Java Native Interface (JNI)
- This interface is used to interact with Native Method Libraries required for the execution and provide the capabilities of such Native Libraries (often written in C/C++). This enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.
- Native Method Libraries
- This is a collection of C/C++ Native Libraries which is required for the Execution Engine and can be accessed through the provided Native Interface.
-
-By Saravanan & Sai Krishna