1.9 KiB
1.9 KiB
Prompt: Node.js Resource Checker (Memory & CPU)
Whenever Copilot generates, modifies, or reviews code in Node.js:
-
Memory Monitoring
- Always remind to check memory usage with:
which returns RSS, Heap Used, Heap Total, External, and Array Buffers.
logger.info(process.memoryUsage()); - If
Heap Usedgrows continuously without freeing, suggest potential memory leaks (unreleased timers, listeners, or large cached data). - If
RSS(Resident Set Size) is unusually higher thanHeap Total, warn that external/native modules might be consuming memory.
- Always remind to check memory usage with:
-
CPU Usage
- Remind to check CPU load using:
or Node.js profiling (
top -p <pid>--inspect,clinic flame). - Flag infinite loops, synchronous blocking calls, or heavy computations running on the main thread as potential CPU overuse.
- Encourage offloading heavy tasks to Workers, child processes, or queues when appropriate.
- Remind to check CPU load using:
-
Best Practices
- Always validate memory and CPU usage after implementing new features.
- Suggest garbage collection checks (
global.gc()) in development when Node is run with--expose-gc. - Warn if long-living references (e.g., large Maps, Sets, caches, or listeners) might not be released.
- Highlight that CPU and memory efficiency are as important as correctness.
-
Output Analysis
- When given memory stats like:
RSS: 186.9MB Heap Used: 67.6MB Heap Total: 71.2MB External: 5.0MB- Compare
Heap UsedvsHeap Total: if close to the limit, risk of OOM. - Compare
RSSvsHeap Total: if RSS is much larger, check for native module or buffer leaks. - If growth is unbounded, warn about potential memory leaks.
- Compare
- When given memory stats like:
-
Always remind to rerun tests with
npx tsc --noEmit(for type safety) and memory checks together, ensuring both correctness and performance.