Skip to content

Conversation

@elharo
Copy link
Contributor

@elharo elharo commented Nov 23, 2025

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.

@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 finished reviewing on behalf of elharo November 23, 2025 16:33
Copy link

Copilot AI left a 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.

@elharo elharo marked this pull request as ready for review November 23, 2025 18:39
@elharo elharo merged commit 7c9e58a into master Nov 23, 2025
36 checks passed
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants