Fix memory leaks: object pooling, REQUEST_SCOPED cache cleanup, and SoftIdentityMap bug #2479
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR addresses critical memory management issues in Maven by implementing automatic object pooling, fixing cache memory leaks, and correcting a serious bug in the SoftIdentityMap implementation.
Problems Addressed
1. Dependency Object Duplication
In large Maven projects, dependency objects are frequently duplicated across different projects and modules, leading to significant memory consumption. The same dependency (e.g.,
org.apache.maven:maven-core:3.9.0
) might be created hundreds or thousands of times across a multi-module build.2. REQUEST_SCOPED Cache Memory Leak
The
DefaultRequestCache
was accumulating REQUEST_SCOPED cache entries indefinitely without ever cleaning them up. These entries were keyed by the outer request but never removed when the request completed, causing a serious memory leak.3. SoftIdentityMap Identity Comparison Bug
The
SoftIdentityMap
had a critical bug where it was usingequals()
instead of identity comparison (==
) in theSoftIdentityReference.equals()
method, causing it to behave like a regular map instead of an identity map.4. InputLocation Object Duplication
InputLocation objects were being created with constructors, missing opportunities for memory optimization through pooling.
Solutions Implemented
🚀 Automatic Dependency Object Pooling
model.vm
) to inject pooling logic intoDependency.Builder.build()
ObjectPool<T>
- Thread-safe object pool with custom equality predicatesDependencyPool
- Global dependency pool with comprehensive equality checkingorg.apache.maven.api.model.Dependency
, not other Dependency classes🔧 REQUEST_SCOPED Cache Cleanup
clearRequestScopedCache()
method toDefaultRequestCache
ModelBuilderSessionImpl
to automatically clear REQUEST_SCOPED cache when requests complete🐛 Fixed SoftIdentityMap Bug
SoftIdentityReference.equals()
to use==
instead ofequals()
📍 InputLocation Factory Methods with Pooling
InputLocation.of()
factory methods to replace constructorsInputLocationPool
for automatic memory optimizationKey Features
Automatic Pooling
Dependency
object is automatically pooled when created viaBuilder.build()
Memory Management
Thread Safety
Comprehensive Equality
groupId
,artifactId
,version
,type
,classifier
,scope
optional
flag,exclusions
list,systemPath
InputLocation
for complete dependency trackingBiPredicate
for flexible comparison strategiesImplementation Details
Generated Code Integration
The
Dependency.Builder.build()
method now automatically pools all dependencies:Cache Cleanup Integration
The
ModelBuilderSessionImpl.build()
method now includes automatic cleanup:SoftIdentityMap Fix
Template Modification
The
src/mdo/model.vm
template was enhanced to inject pooling forDependency
objects in the correct package:Files Added/Modified
Core Implementation
api/maven-api-model/src/main/java/org/apache/maven/api/model/ObjectPool.java
- Generic thread-safe object poolapi/maven-api-model/src/main/java/org/apache/maven/api/model/DependencyPool.java
- Dependency-specific pool with comprehensive equalitysrc/mdo/java/ObjectPool.java
- Template for ObjectPool generationsrc/mdo/java/InputLocation.java
- MODIFIED to add factory methods and poolingTemplate Enhancement
src/mdo/model.vm
- MODIFIED to inject automatic pooling intoDependency.Builder.build()
with precise package targetingCache Fixes
impl/maven-impl/src/main/java/org/apache/maven/impl/cache/DefaultRequestCache.java
- ENHANCED with cleanup methods and debuggingimpl/maven-impl/src/main/java/org/apache/maven/impl/cache/SoftIdentityMap.java
- FIXED identity comparison bugimpl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
- MODIFIED to add cache cleanupTests
api/maven-api-model/src/test/java/org/apache/maven/api/model/DependencyPoolTest.java
- Comprehensive tests for automatic poolingUsage Examples
Automatic Pooling (No Code Changes)
InputLocation Factory Methods
Cache Monitoring
Performance Characteristics
Memory Benefits
CPU Trade-offs
Scalability
Testing
Comprehensive Test Coverage
Test Results
Backward Compatibility
Future Extensions
This foundation can be extended to other model objects:
The template-based approach makes it easy to add pooling to any generated model class by adding similar conditions to the
model.vm
template.Benefits Summary
For Users
For Developers
For Maven Project
Summary of Changes
ObjectPool<T>
andDependencyPool
in model packagemodel.vm
with automatic pooling injection and precise targetingDependency.Builder.build()
now includes automatic poolingThis implementation provides a comprehensive solution for Maven's memory management issues, addressing both object duplication and cache memory leaks while maintaining complete backward compatibility. The changes are transparent to users but provide significant memory benefits and fix critical memory leaks that were affecting large Maven builds.