Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

  • Replace the ThreadStatic field _threadInstance with a WeakReference to prevent memory leaks
  • Update the ThreadInstance property to properly handle the WeakReference pattern
  • Maintain all existing functionality while allowing garbage collection of unused ArrayPool instances

Details

This change addresses issue #99 by replacing the strong reference ThreadStatic field with a WeakReference. The original implementation kept ArrayPool instances alive for the entire thread lifetime, which could lead to memory leaks in long-running applications.

Changes Made

  • Changed _threadInstance field from ArrayPool<T> to WeakReference<ArrayPool<T>>
  • Updated ThreadInstance property to use TryGetTarget() method for safe weak reference access
  • Added fallback logic to create a new ArrayPool instance when the weak reference is collected

Benefits

  • Memory leak prevention: ArrayPool instances can now be garbage collected when memory pressure is high
  • Backward compatibility: All existing APIs continue to work without changes
  • Thread safety: ThreadStatic behavior is preserved
  • Performance: Minimal overhead added for memory management benefits

Testing

  • All existing unit tests pass (20/20)
  • Build completes successfully
  • No breaking changes to the public API

Fixes #99

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #99
@konard konard self-assigned this Sep 13, 2025
… field

This change prevents memory leaks by allowing the garbage collector to
collect ArrayPool instances when memory pressure is high, instead of
keeping them alive for the entire thread lifetime.

Fixes #99

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Use week references here to avoid memory leaks Use WeakReference to avoid memory leaks in ArrayPool ThreadStatic field Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use week references here to avoid memory leaks

2 participants