2.0 KiB
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
- Always use
unref()
on intervals that shouldn't keep the process alive - Always provide cleanup/destroy methods for classes that create timers
- Implement size limits on all caches and Maps
- Consider using WeakMap for caches where appropriate
- Run memory leak tests regularly, especially after adding new features
Running the Memory Leak Test
# 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.