[low-priority] Close bridged instruments when GC'd#16219
Merged
laurit merged 9 commits intoopen-telemetry:mainfrom Mar 30, 2026
Merged
[low-priority] Close bridged instruments when GC'd#16219laurit merged 9 commits intoopen-telemetry:mainfrom
laurit merged 9 commits intoopen-telemetry:mainfrom
Conversation
65b1b94 to
72ebbb8
Compare
laurit
reviewed
Mar 3, 2026
| Consumer<application.io.opentelemetry.api.metrics.ObservableDoubleMeasurement> | ||
| applicationCallback) { | ||
| return new ApplicationObservableDoubleCounter( | ||
| agentBuilder.buildWithCallback( |
Contributor
There was a problem hiding this comment.
should we also handle calling close on the returned ObservableDoubleCounter and remove the callback from the list so it could be collected immediately?
laurit
reviewed
Mar 3, 2026
817261c to
4c03a99
Compare
laurit
approved these changes
Mar 10, 2026
laurit
reviewed
Mar 10, 2026
|
|
||
| // Anchors callbacks to this class's lifecycle. Since this class is injected as a helper into each | ||
| // application class loader, callbacks are naturally tied to their class loader's lifecycle. | ||
| private static final Set<Object> callbacks = ConcurrentHashMap.newKeySet(); |
Contributor
There was a problem hiding this comment.
Ideally I think this should be an identity set to guard agains callbacks with weird equals/hashCode. To handle using the same callback for multiple instruments where one of the instruments gets closed it would need to use ref counting. I think we can ignore these for now.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently, when the agent bridges an async instrument callback (e.g.
buildWithCallback), the SDKends up holding a strong reference chain back to the app classloader:
This pins the app classloader forever, leaking memory on tomcat webapp redeploys.
This PR breaks the chain with a
WeakReference, then anchors the bridging lambda to the app classloader's own lifecycle so it stays alive exactly as long as the classloader does:(all good ideas here are credit to @laurit, all bad implementation details are credit to me)