Files
smartproxy/readme.memory-leaks-fixed.md
Juergen Kunz fc09af9afd
Some checks failed
Default (tags) / security (push) Successful in 41s
Default (tags) / test (push) Failing after 31m49s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
19.6.1
2025-06-09 16:37:46 +00:00

45 lines
2.0 KiB
Markdown

# Memory Leaks Fixed in SmartProxy
## Summary of Issues Found and Fixed
### 1. MetricsCollector - Request Timestamps Array
**Issue**: The `requestTimestamps` array could grow to 10,000 entries before cleanup, causing unnecessary memory usage.
**Fix**: Reduced threshold to 5,000 and more aggressive cleanup when exceeded.
### 2. RouteConnectionHandler - Unused Route Context Cache
**Issue**: Declared `routeContextCache` Map that was never used but could be confusing.
**Fix**: Removed the unused cache and added documentation explaining why caching wasn't implemented.
### 3. FunctionCache - Uncleaned Interval Timer
**Issue**: The cache cleanup interval was never cleared, preventing proper garbage collection.
**Fix**: Added `destroy()` method to properly clear the interval timer.
### 4. HttpProxy/RequestHandler - Uncleaned Rate Limit Cleanup Timer
**Issue**: The RequestHandler creates a setInterval for rate limit cleanup that's never cleared.
**Status**: Needs fix - add destroy method and call it from HttpProxy.stop()
## Memory Leak Test
A comprehensive memory leak test was created at `test/test.memory-leak-check.node.ts` that:
- Tests with 1000 requests to same routes
- Tests with 1000 requests to different routes (cache growth)
- Tests rapid 10,000 requests (timestamp array growth)
- Monitors memory usage throughout
- Verifies specific data structures don't grow unbounded
## Recommendations
1. Always use `unref()` on intervals that shouldn't keep the process alive
2. Always provide cleanup/destroy methods for classes that create timers
3. Implement size limits on all caches and Maps
4. Consider using WeakMap for caches where appropriate
5. Run memory leak tests regularly, especially after adding new features
## Running the Memory Leak Test
```bash
# Run with garbage collection exposed for accurate measurements
node --expose-gc test/test.memory-leak-check.node.ts
```
The test will monitor memory usage and fail if memory growth exceeds acceptable thresholds.