JVM Class Loader Code Sharing via Reentrant Barriers
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
The Java Virtual Machine (JVM) faces inefficiencies in memory consumption and dynamic compilation costs due to the replication of runtime representations for each class loader, especially when defining multiple classes, which can lead to error-prone delegation relationships and inadequate isolation of software components.
Innovation Solution
The solution involves generating native code that implements class initialization and link resolution barriers, as well as retrieving loader-dependent information from a loader-dependent table, to share dynamically compiled code between different class loaders, thereby optimizing memory usage and amortizing compilation costs.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Reliability
If the JVM replicates the entire runtime representation of a class in memory for each class loader, then class isolation and namespace separation are achieved, but main memory consumption increases significantly
Solution Approach 1:
The runtime representation of a class is segmented into two distinct parts: a shared portion that can be common across multiple class loaders, and a loader-dependent portion that maintains isolation. This segmentation allows the JVM to reduce memory consumption by avoiding complete replication while preserving the necessary isolation properties through the loader-dependent segment.
Solution Approach 2:
The shared portion of the runtime representation is designed to be universal and reusable across multiple class loaders. This single instance serves multiple functions by supporting isolation requirements for different class loaders simultaneously, eliminating the need for separate complete copies and thereby reducing overall memory consumption.
2Reliability
If multiple class loaders define the same class separately, then namespace separation and code isolation are maintained, but dynamic compilation costs are replicated for each loader
Solution Approach 1:
The dynamic compilation process is merged across multiple class loaders by creating a single compiled native code version that is shared among them. Instead of each class loader independently compiling the same bytecode, the compilation results are combined into a common shared portion, amortizing the compilation costs while maintaining loader-specific execution through the loader-dependent portion.
Solution Approach 2:
The dynamic compilation is performed once in advance for the shared portion of the runtime representation, before being reused by multiple class loaders. This preliminary compilation action eliminates the need for repeated compilation operations, significantly reducing the time and computational resources spent on dynamic compilation across multiple loaders.
3Quantity of substance
If class loaders use delegation relationships to share compiled code, then memory consumption is reduced, but the complexity and error-proneness of delegation relationships increases
Solution Approach 1:
The complex delegation logic is extracted and replaced by a simpler structure where class loaders directly reference a shared runtime representation. By taking out the intermediate delegation mechanisms and establishing direct references to the shared portion, the system reduces memory consumption while avoiding the complexity and error-proneness associated with managing delegation relationships.
Data Source
AI summary
A method for sharing dynamically compiled code between different class loaders is provided. In this method, loader-reentrant compiled code is produced from bytecodes by generating native code implementing a class initialization barrier when compiling bytecodes that require a class to be initialized, by generating native code implementing a link resolution barrier when compiling bytecodes that require a symbolic link to be resolved, and by generating code to retrieve loader-dependent data from a loader-dependent table when compiling bytecodes that use data computed from a resolved symbolic link.


