Fix memory leak from cached xarray wrappers in CellMapImage#64
Fix memory leak from cached xarray wrappers in CellMapImage#64rhoadesScholar merged 21 commits intomainfrom
Conversation
Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request fixes a critical memory leak in CellMapImage where cached xarray wrappers accumulated during training loops, causing memory consumption to grow from 0 to nearly 500GB over 20 minutes despite 350MB batches. The fix adds explicit cache clearing after each data retrieval operation while preserving TensorStore's chunk cache performance.
Changes:
- Adds
_clear_array_cache()method to remove cached xarray wrapper from__dict__after each__getitem__call - Preserves TensorStore connection and chunk cache for performance
- Includes comprehensive test suite and documentation
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/cellmap_data/image.py | Implements cache clearing mechanism by adding _clear_array_cache() method and calling it after data retrieval in __getitem__(), plus updated docstrings |
| tests/test_memory_management.py | New comprehensive test suite verifying cache clearing behavior across different scenarios including spatial transforms, value transforms, and interpolation |
| MEMORY_LEAK_FIX.md | Detailed documentation explaining the root cause, solution, and architectural considerations |
| tests/demo_memory_fix.py | Demo script showing the fix (though it only prints information rather than executing actual demonstrations) |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Implemented cache clearing in __getitem__ to prevent memory accumulation. - Added tests to verify cache clearing behavior during simulated training loops. - Updated demo script for memory profiling and added objgraph support. - Moved read limiter to utils for better organization.
…etWriter and ImageWriter
…taset and CellMapMultiDataset
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
…and use executor pattern Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
…ed warning objects Co-authored-by: rhoadesScholar <37990507+rhoadesScholar@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64 +/- ##
==========================================
+ Coverage 70.48% 71.83% +1.34%
==========================================
Files 28 28
Lines 2450 2464 +14
==========================================
+ Hits 1727 1770 +43
+ Misses 723 694 -29 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.