()
| 16 | * Polls every 10 seconds; returns null while status is 'normal'. |
| 17 | */ |
| 18 | export function useMemoryUsage(): MemoryUsageInfo | null { |
| 19 | const [memoryUsage, setMemoryUsage] = useState<MemoryUsageInfo | null>(null) |
| 20 | |
| 21 | useInterval(() => { |
| 22 | const heapUsed = process.memoryUsage().heapUsed |
| 23 | const status: MemoryUsageStatus = |
| 24 | heapUsed >= CRITICAL_MEMORY_THRESHOLD |
| 25 | ? 'critical' |
| 26 | : heapUsed >= HIGH_MEMORY_THRESHOLD |
| 27 | ? 'high' |
| 28 | : 'normal' |
| 29 | setMemoryUsage(prev => { |
| 30 | // Bail when status is 'normal' — nothing is shown, so heapUsed is |
| 31 | // irrelevant and we avoid re-rendering the whole Notifications subtree |
| 32 | // every 10 seconds for the 99%+ of users who never reach 1.5GB. |
| 33 | if (status === 'normal') return prev === null ? prev : null |
| 34 | return { heapUsed, status } |
| 35 | }) |
| 36 | }, 10_000) |
| 37 | |
| 38 | return memoryUsage |
| 39 | } |
| 40 |
no test coverage detected