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