Perf(custom): add memory monitor and optimiz memory usage

This commit is contained in:
Kuingsmile
2025-08-13 15:11:55 +08:00
parent 11341c024a
commit f0b0e55937
7 changed files with 144 additions and 79 deletions

View File

@@ -62,8 +62,8 @@ const trayWindowOptions = {
preload: preloadPath,
nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
backgroundThrottling: false,
nodeIntegrationInWorker: false,
backgroundThrottling: true,
webSecurity: false
}
}
@@ -83,11 +83,11 @@ const settingWindowOptions = {
webPreferences: {
sandbox: false,
webviewTag: true,
backgroundThrottling: false,
backgroundThrottling: true,
preload: preloadPath,
nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
nodeIntegrationInWorker: false,
webSecurity: false
}
} as IBrowserWindowOptions
@@ -112,8 +112,8 @@ const miniWindowOptions = {
preload: preloadPath,
nodeIntegration: false,
contextIsolation: true,
backgroundThrottling: false,
nodeIntegrationInWorker: true
backgroundThrottling: true,
nodeIntegrationInWorker: false
}
} as IBrowserWindowOptions
@@ -133,7 +133,7 @@ const renameWindowOptions = {
preload: preloadPath,
nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
nodeIntegrationInWorker: false,
backgroundThrottling: false
}
} as IBrowserWindowOptions
@@ -158,11 +158,11 @@ const toolboxWindowOptions = {
icon: logo,
webPreferences: {
sandbox: false,
backgroundThrottling: false,
backgroundThrottling: true,
preload: preloadPath,
nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
nodeIntegrationInWorker: false,
webSecurity: false
}
} as IBrowserWindowOptions

View File

@@ -1,5 +1,31 @@
import { EventEmitter } from 'node:events'
const bus = new EventEmitter()
class OptimizedBus extends EventEmitter {
constructor () {
super()
this.setMaxListeners(50)
}
once (event: string | symbol, listener: (...args: any[]) => void): this {
const wrappedListener = (...args: any[]) => {
try {
listener(...args)
} finally {
this.removeListener(event, wrappedListener)
}
}
return super.once(event, wrappedListener)
}
cleanupListeners () {
const events = this.eventNames()
events.forEach(event => {
const listenerCount = this.listenerCount(event)
console.log(` listener count (${listenerCount}) for event: ${String(event)}`)
})
}
}
const bus = new OptimizedBus()
export default bus

View File

@@ -34,6 +34,7 @@ import { II18nLanguage, IRemoteNoticeTriggerHook, ISartMode, IWindowList } from
import { getUploadFiles } from '~/utils/handleArgv'
import { initI18n } from '~/utils/handleI18n'
import { notificationList } from '~/utils/notification'
import { MemoryMonitor } from '~/utils/performanceOptimizer'
import { CLIPBOARD_IMAGE_FOLDER } from '~/utils/static'
import updateChecker from '~/utils/updateChecker'
@@ -164,6 +165,10 @@ class LifeCycle {
initI18n()
rpcServer.start()
busEventList.listen()
if (process.env.NODE_ENV === 'development') {
MemoryMonitor.start(30000)
}
}
#onReady () {
@@ -315,6 +320,7 @@ class LifeCycle {
server.shutdown()
webServer.stop()
stopFileServer()
MemoryMonitor.stop()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {

View File

@@ -15,7 +15,7 @@ class ClipboardWatcher extends EventEmitter {
this.timer = null
}
startListening (watchDelay = 500) {
startListening (watchDelay = 1000) {
this.stopListening(false)
this.timer = setInterval(() => {

View File

@@ -70,7 +70,6 @@ export const showNotification = (
const notification = new Notification({
title: options.title,
body: options.body
// icon: options.icon || undefined
})
const handleClick = () => {
if (options.clickToCopy) {

View File

@@ -0,0 +1,31 @@
export class MemoryMonitor {
// eslint-disable-next-line no-undef
private static interval: NodeJS.Timeout | null = null
static start (intervalMs: number = 30000) {
if (this.interval) return
this.interval = setInterval(() => {
const memUsage = process.memoryUsage()
const mbUsage = {
rss: Math.round(memUsage.rss / 1024 / 1024),
heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024),
heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024),
external: Math.round(memUsage.external / 1024 / 1024)
}
console.log(`[Memory] RSS: ${mbUsage.rss}MB, Heap: ${mbUsage.heapUsed}/${mbUsage.heapTotal}MB, External: ${mbUsage.external}MB`)
if (mbUsage.heapUsed / mbUsage.heapTotal > 0.8 && global.gc) {
console.log('[Memory] Triggering garbage collection')
global.gc()
}
}, intervalMs)
}
static stop () {
if (this.interval) {
clearInterval(this.interval)
this.interval = null
}
}
}

View File

@@ -7,7 +7,10 @@ import { IWindowList } from '~/utils/enum'
export function openMiniWindow (hideSettingWindow: boolean = true) {
const miniWindow = windowManager.get(IWindowList.MINI_WINDOW)!
miniWindow.removeAllListeners()
miniWindow.removeAllListeners('close')
miniWindow.removeAllListeners('move')
if (db.get(configPaths.settings.miniWindowOntop)) {
miniWindow.setAlwaysOnTop(true)
}