-
Notifications
You must be signed in to change notification settings - Fork 666
Fix CLASS_LOADER_TYPE_CACHE OOM issue with WeakHashMap #772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Replace hashCode-based cache with WeakHashMap to automatically cleanup dynamic class caches when ClassLoader is garbage collected. - Change CLASS_LOADER_TYPE_CACHE from Map<Integer, ...> to Map<ClassLoader, ...> - Use Collections.synchronizedMap(new WeakHashMap<>()) for automatic cleanup - Add separate BOOTSTRAP_TYPE_CACHE for null ClassLoader handling - Add comprehensive unit tests validating cache behavior Fixes OOM issues in rule engine scenarios like Drools where dynamic classes are frequently created and ClassLoaders are recycled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an OutOfMemoryError issue in the SWDescriptionStrategy cache by replacing a hashCode-based cache with WeakHashMap for automatic cleanup when ClassLoaders are garbage collected. This addresses memory leaks in scenarios involving dynamic class generation like Drools rule engines.
- Replaces HashMap with WeakHashMap to automatically clean up cache entries when ClassLoaders are GC'd
- Adds separate handling for bootstrap ClassLoader since WeakHashMap doesn't support null keys
- Includes comprehensive test coverage for the new cache behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| SWDescriptionStrategy.java | Implements WeakHashMap-based cache and bootstrap ClassLoader handling |
| SWDescriptionStrategyCacheTest.java | Adds test coverage for WeakHashMap cleanup, bootstrap handling, and concurrent access |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...ytebuddy-patch/src/test/java/net/bytebuddy/agent/builder/SWDescriptionStrategyCacheTest.java
Outdated
Show resolved
Hide resolved
...ytebuddy-patch/src/test/java/net/bytebuddy/agent/builder/SWDescriptionStrategyCacheTest.java
Outdated
Show resolved
Hide resolved
...ytebuddy-patch/src/test/java/net/bytebuddy/agent/builder/SWDescriptionStrategyCacheTest.java
Show resolved
Hide resolved
|
@gaojingyuan001 @hyhyf This one. |
…builder/SWDescriptionStrategyCacheTest.java Co-authored-by: Copilot <[email protected]>
...ytebuddy-patch/src/test/java/net/bytebuddy/agent/builder/SWDescriptionStrategyCacheTest.java
Outdated
Show resolved
Hide resolved
- Convert all Chinese comments to English for better international collaboration - Maintain original functionality and test logic - Improve code readability for global contributors
|
@gaojingyuan001 @hyhyf Any of you tried this one? This PR was raised due to your use cases. |
|
@kylixs As this is an enhancement and it seems no harm to the whole system, could you update the changes and we consider to merge this if there is no further feedback? |
Problem
The CLASS_LOADER_TYPE_CACHE in SWDescriptionStrategy uses a two-level cache structure with ClassLoader.hashCode() as the first-level key and type
name as the second-level key. In scenarios involving rule engines like Drools that dynamically generate large numbers of classes, this cache grows
indefinitely and causes OutOfMemoryError during long-running applications.
Original Discussion, apache/skywalking#12627
Root Cause
Solution
Replace the hashCode-based cache with a WeakHashMap that automatically cleans up entries when ClassLoaders are garbage collected:
Key Changes
CHANGESlog.