From 38713d4aa9cd9037aab73a84ad8d2d0f91032b1f Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 18 Nov 2022 15:49:01 +0800 Subject: [PATCH] AppDelegate // Add checkMemoryUsage(). --- Source/Modules/AppDelegate.swift | 27 ++++++++++++++++++- .../Resources/Base.lproj/Localizable.strings | 1 + Source/Resources/en.lproj/Localizable.strings | 1 + Source/Resources/ja.lproj/Localizable.strings | 1 + .../zh-Hans.lproj/Localizable.strings | 1 + .../zh-Hant.lproj/Localizable.strings | 1 + 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index 9ea59ef7..17a08a6c 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -9,6 +9,7 @@ // requirements defined in MIT License. import FolderMonitor +import Shared import Uninstaller import UpdateSputnik @@ -48,7 +49,8 @@ extension AppDelegate { userNotification.title = NSLocalizedString("vChewing", comment: "") userNotification.informativeText = NSLocalizedString( "vChewing crashed while handling previously loaded UOM observation data. These data files are cleaned now to ensure the usability.", - comment: "") + comment: "" + ) userNotification.soundName = NSUserNotificationDefaultSoundName NSUserNotificationCenter.default.deliver(userNotification) } @@ -105,6 +107,29 @@ extension AppDelegate { NSApp.setActivationPolicy(.accessory) } + /// 檢查該程式本身的記憶體佔用量。 + /// - Returns: 記憶體佔用量(MiB)。 + @discardableResult public func checkMemoryUsage() -> Double { + guard let currentMemorySizeInBytes = NSApplication.memoryFootprint else { return 0 } + let currentMemorySize: Double = (Double(currentMemorySizeInBytes) / 1024 / 1024).rounded(toPlaces: 1) + switch currentMemorySize { + case 300...: + vCLog("WARNING: EXCESSIVE MEMORY FOOTPRINT (\(currentMemorySize)MB).") + let userNotification = NSUserNotification() + userNotification.title = NSLocalizedString("vChewing", comment: "") + userNotification.informativeText = NSLocalizedString( + "vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size.", + comment: "" + ) + NSUserNotificationCenter.default.deliver(userNotification) + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + NSApp.terminate(self) + } + default: break + } + return currentMemorySize + } + // New About Window @IBAction public func about(_: Any) { CtlAboutWindow.show() diff --git a/Source/Resources/Base.lproj/Localizable.strings b/Source/Resources/Base.lproj/Localizable.strings index fe6c5a13..df654f82 100644 --- a/Source/Resources/Base.lproj/Localizable.strings +++ b/Source/Resources/Base.lproj/Localizable.strings @@ -1,4 +1,5 @@ "vChewing" = "vChewing"; +"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size." = "vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size."; "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key." = "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key."; "Wildcard key cannot be the initial key." = "Wildcard key cannot be the initial key."; "CIN Cassette Mode" = "CIN Cassette Mode"; diff --git a/Source/Resources/en.lproj/Localizable.strings b/Source/Resources/en.lproj/Localizable.strings index fe6c5a13..df654f82 100644 --- a/Source/Resources/en.lproj/Localizable.strings +++ b/Source/Resources/en.lproj/Localizable.strings @@ -1,4 +1,5 @@ "vChewing" = "vChewing"; +"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size." = "vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size."; "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key." = "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key."; "Wildcard key cannot be the initial key." = "Wildcard key cannot be the initial key."; "CIN Cassette Mode" = "CIN Cassette Mode"; diff --git a/Source/Resources/ja.lproj/Localizable.strings b/Source/Resources/ja.lproj/Localizable.strings index f69f3b55..d66ffcc4 100644 --- a/Source/Resources/ja.lproj/Localizable.strings +++ b/Source/Resources/ja.lproj/Localizable.strings @@ -1,4 +1,5 @@ "vChewing" = "威注音入力アプリ"; +"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size." = "威注音入力アプリはメモリー使用量多すぎたため、再起動しました。もし、よろしければ、この問題について、開発元にご連絡をし、そしてこのパソコンの CPU 種類情報(Intel か、Apple Silicon か)も一緒に教えてください。今のメモリー使用量は NSLog にて記録済みである。"; "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key." = "このメニューのお用いには、ご自分でユーザー辞書で\n「_punctuation_list」キーの符号記録のご補充が必要でござる。"; "Wildcard key cannot be the initial key." = "骨牌キーは最初のキーとしてはならぬ。"; "CIN Cassette Mode" = "CIN カセットモード"; diff --git a/Source/Resources/zh-Hans.lproj/Localizable.strings b/Source/Resources/zh-Hans.lproj/Localizable.strings index 8c85a84c..36d67067 100644 --- a/Source/Resources/zh-Hans.lproj/Localizable.strings +++ b/Source/Resources/zh-Hans.lproj/Localizable.strings @@ -1,4 +1,5 @@ "vChewing" = "威注音输入法"; +"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size." = "威注音输入法因记忆体用量过大的问题而重新启动。如果您方便的话,请就这个问题知会该输入法的主程式师、且请务必说明自己在用的是 Intel 机种还是 Apple Silicon 机种。当前输入法记忆体用量已借由 NSLog 记录输出。"; "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key." = "请自行以「_punctuation_list」索引键\n在使用者自订语汇档案内扩充该符号选单的内容。";"Wildcard key cannot be the initial key." = "花牌键不得作为起始码。"; "Wildcard key cannot be the initial key." = "花牌键不得作为起始码。"; "CIN Cassette Mode" = "CIN 磁带模式"; diff --git a/Source/Resources/zh-Hant.lproj/Localizable.strings b/Source/Resources/zh-Hant.lproj/Localizable.strings index 90d7f251..0bb702dd 100644 --- a/Source/Resources/zh-Hant.lproj/Localizable.strings +++ b/Source/Resources/zh-Hant.lproj/Localizable.strings @@ -1,4 +1,5 @@ "vChewing" = "威注音輸入法"; +"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size." = "威注音輸入法因記憶體用量過大的問題而重新啟動。如果您方便的話,請就這個問題知會該輸入法的主程式師、且請務必說明自己在用的是 Intel 機種還是 Apple Silicon 機種。當前輸入法記憶體用量已藉由 NSLog 記錄輸出。"; "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key." = "請自行以「_punctuation_list」索引鍵\n在使用者自訂語彙檔案內擴充該符號選單的內容。"; "Wildcard key cannot be the initial key." = "花牌鍵不得作為起始碼。"; "CIN Cassette Mode" = "CIN 磁帶模式";