GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Plug ThreadLocal memory leak by elharo · Pull Request #1276 · apache/maven-assembly-plugin

Conversation

@elharo

@elharo elharo commented Nov 23, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Gemini explains:

Internal Map Structure:

Each Thread object in Java maintains an internal ThreadLocalMap to store the values associated with ThreadLocal instances for that specific thread. This map uses ThreadLocal instances as keys and the stored values as values.

set(null) vs. remove():
When ThreadLocal.set(null) is called, the entry in the ThreadLocalMap associated with the current ThreadLocal instance is updated, setting its value to null. However, the key (the ThreadLocal instance itself) and the entry in the map remain present.

When ThreadLocal.remove() is called, the entire entry (key and value) for that specific ThreadLocal instance is removed from the ThreadLocalMap.

Memory Leak Scenario:
In a thread pool, threads are reused across multiple tasks or requests. If a ThreadLocal value is set and then cleared with set(null), the ThreadLocal instance (and potentially its classloader, if it's an application-specific ThreadLocal) remains referenced within the thread's ThreadLocalMap.

If the application that deployed the ThreadLocal is later undeployed, but the thread from the pool continues to exist, the ThreadLocal instance and its associated classloader can prevent the garbage collection of the undeployed application's classes and resources. This constitutes a memory leak, as the memory used by the undeployed application cannot be reclaimed.

To prevent this memory leak, it is crucial to use ThreadLocal.remove() instead of ThreadLocal.set(null) when you want to clear a ThreadLocal value and ensure proper cleanup. This explicitly removes the entry from the ThreadLocalMap, allowing for proper garbage collection of the ThreadLocal instance and its associated resources.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
elharo added 2 commits November 23, 2025 10:28
@elharo
Plug memory leak
939ad7d
@elharo
spotless
5932991
@elharo
elharo requested review from Copilot and slachiewicz November 23, 2025 16:32
@elharo elharo changed the title Plug memory leak Plug ThreadLocal memory leak Nov 23, 2025
@elharo elharo added the bug Something isn't working label Nov 23, 2025
Copilot started reviewing on behalf of elharo November 23, 2025 16:32
View session
Copilot finished reviewing on behalf of elharo November 23, 2025 16:33
Copilot AI reviewed Nov 23, 2025
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown

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 addresses a potential memory leak by replacing ThreadLocal.set(null) with ThreadLocal.remove() throughout the AssemblyProxyArchiver class. Using remove() ensures that ThreadLocal entries are completely removed from the thread's internal map, preventing classloader memory leaks in thread pool environments where threads are reused.

  • Replaced all 33 occurrences of inPublicApi.set(null) with inPublicApi.remove() in finally blocks
  • Ensures proper ThreadLocal cleanup to prevent memory leaks in long-running applications
  • No functional behavior changes, only improved resource management

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@elharo
elharo marked this pull request as ready for review November 23, 2025 18:39
slachiewicz
slachiewicz approved these changes Nov 23, 2025
View reviewed changes
@elharo
elharo merged commit 7c9e58a into master Nov 23, 2025
@elharo
elharo deleted the combine branch November 23, 2025 19:28
@github-actions github-actions Bot added this to the 3.8.1 milestone Nov 23, 2025
@github-actions github-actions Bot assigned elharo Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@slachiewicz slachiewicz slachiewicz approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@elharo elharo

Labels

bug Something isn't working

Projects

None yet

Milestone

3.8.1

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@elharo @slachiewicz
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.