Files
tk-electron-ai/main/utils/mem.js
2025-10-28 19:40:13 +08:00

26 lines
824 B
JavaScript

// main/utils/mem.js
const { app } = require('electron')
function toMB(v) {
if (!v || v <= 0) return 0
const kbToMB = v / 1024
if (kbToMB > 1) return Number(kbToMB.toFixed(1))
return Number(v.toFixed(1))
}
function dumpAllMem() {
try {
const metrics = app.getAppMetrics()
const report = metrics.map(m => {
const mem = m.memory || {}
const workingSetMB = toMB(mem.workingSetSize ?? mem.workingSet ?? 0)
const privateMB = toMB(mem.privateBytes ?? mem.private ?? 0)
const sharedMB = toMB(mem.shared ?? 0)
return { pid: m.pid, type: m.type, workingSetMB, privateMB, sharedMB }
})
// console.log(report)
} catch (e) {
console.warn('getAppMetrics error:', e)
}
}
module.exports = { toMB, dumpAllMem }