memory-leak-detection
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-14
- Stars
- ★ 13
- Forks
- 2
03 SKILL.md 正文
/memory-leak-detection
When to Use
- Long-running agent sessions accumulating heap without releasing
- Debugging why sandbox process is OOM-killed after hours of operation
- Validating that event listeners and streams are properly cleaned up
- Heap diffing to identify which objects grow between requests
Do NOT use for
- One-shot scripts (leak only matters for long-running processes)
- Production traffic interception (heap snapshots block the event loop)
Heap leak detector (memwatch-next)
import memwatch from 'memwatch-next'
// 'leak' fires when heap grows 5 consecutive GC cycles
memwatch.on('leak', (info) => {
console.error('[memwatch] LEAK DETECTED', JSON.stringify(info))
// info: { start, end, growth, reason }
// growth is bytes added since last GC
})
// GC stats after every garbage collection
memwatch.on('stats', (stats) => {
if (stats.current_base > 200 * 1024 * 1024) { // 200MB threshold
console.warn('[memwatch] heap baseline high:', Math.round(stats.current_base / 1024**2), 'MB')
}
})
Heap diff to find leaked objects
import memwatch from 'memwatch-next'
// Perform an action twice, diff heap to see what objects grew
const hd = new memwatch.HeapDiff()
// ... run the suspected leaky operation ...
await runAgentSession()
const diff = hd.end()
const growing = diff.change.details
.filter(d => d.size_bytes > 0)
.sort((a, b) => b.size_bytes - a.size_bytes)
.slice(0, 10)
console.table(growing.map(d => ({
what: d.what,
added: d['+'],
freed: d['-'],
deltaBytes: d.size_bytes,
})))
V8 heap snapshot (built-in, no dependency)
import v8 from 'v8'
import { writeFileSync } from 'fs'
// Take a heap snapshot — blocks event loop, do offline/on-demand only
function takeHeapSnapshot(path = `/tmp/heap-${Date.now()}.heapsnapshot`): string {
const snapshot = v8.writeHeapSnapshot(path)
console.log(`[heap] snapshot written: ${snapshot}`)
return snapshot
// Open in Chrome DevTools → Memory tab → Load snapshot
}
Simple heap trending (no library)
const heapSamples: number[] = []
function sampleHeap(): void {
const used = process.memoryUsage().heapUsed
heapSamples.push(used)
if (heapSamples.length > 10) {
const trend = heapSamples.slice(-10)
const growing = trend.every((v, i) => i === 0 || v >= trend[i-1])
if (growing) {
const growthMb = (trend.at(-1)! - trend[0]) / 1024**2
console.warn(`[heap] steady growth: +${growthMb.toFixed(1)}MB over last 10 samples`)
}
heapSamples.splice(0, heapSamples.length - 10)
}
}
setInterval(sampleHeap, 30_000)
Common leak patterns to fix
// Leak 1: event listener not removed
const handler = () => { /* ... */ }
emitter.on('data', handler)
// Fix:
emitter.once('data', handler) // or removeListener on cleanup
// Leak 2: setInterval not cleared
const id = setInterval(() => { /* ... */ }, 1000)
// Fix:
process.on('exit', () => clearInterval(id))
// Leak 3: large object in closure
// function factory() { const big = new Array(1e6).fill(0); return () => big[0] }
// Fix: don't capture large objects in long-lived closures
Anti-Fake-Pass Checklist
❌ heap snapshot in production → blocks event loop for 2-30 seconds
❌ memwatch-next requires native build → fails on ARM without node-gyp
❌ HeapDiff across async boundaries → GC between start and end confuses diff
❌ process.memoryUsage().heapUsed alone → includes GC lag, not true live baseline
❌ No threshold → leak detector fires on normal warmup growth
❌ Listening to 'leak' but not acting → log fills, no remediation
04 同仓库更多 Skills
openai--sentry--sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
ito-basket-compare
Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.
implementing-network-traffic-baselining
Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling
detecting-evasion-techniques-in-endpoint-logs
Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.
implementing-microsegmentation-with-guardicore
Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.
ramdisk-tmpfs-patterns
暂无简介