MCPcopy Create free account
hub / github.com/Noumena-Network/code / startSdkMemoryMonitor

Function startSdkMemoryMonitor

src/utils/sdkHeapDumpMonitor.ts:18–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16}
17
18export function startSdkMemoryMonitor(): void {
19 if (started) {
20 return
21 }
22
23 started = true
24 let dumpCount = 0
25 let lastDumpAt = 0
26 let inFlight = false
27
28 interval = setInterval(() => {
29 if (inFlight || dumpCount >= MAX_AUTO_DUMPS_PER_SESSION) {
30 return
31 }
32
33 const rss = process.memoryUsage().rss
34 if (rss < RSS_THRESHOLD_BYTES) {
35 return
36 }
37
38 const now = Date.now()
39 if (now - lastDumpAt < DUMP_COOLDOWN_MS) {
40 return
41 }
42
43 dumpCount += 1
44 const dumpNumber = dumpCount
45 lastDumpAt = now
46 inFlight = true
47
48 logForDebugging(
49 `[HeapDump] RSS ${formatGigabytes(rss)} GB exceeded 1.5 GB threshold; capturing auto heap dump #${dumpNumber}`,
50 { level: 'warn' },
51 )
52
53 void performHeapDump('auto-1.5GB', dumpNumber)
54 .then(result => {
55 if (!result.success) {
56 logForDebugging(
57 `[HeapDump] Auto heap dump #${dumpNumber} failed: ${result.error ?? 'unknown error'}`,
58 { level: 'error' },
59 )
60 }
61 })
62 .catch(error => {
63 logForDebugging(
64 `[HeapDump] Auto heap dump #${dumpNumber} threw: ${String(error)}`,
65 { level: 'error' },
66 )
67 })
68 .finally(() => {
69 inFlight = false
70 })
71 }, CHECK_INTERVAL_MS)
72
73 interval.unref?.()
74
75 if (!cleanupRegistered) {

Callers

nothing calls this directly

Calls 4

formatGigabytesFunction · 0.85
performHeapDumpFunction · 0.85
registerCleanupFunction · 0.85
logForDebuggingFunction · 0.70

Tested by

no test coverage detected