From c6d00355fcd1d915eee9d5678669ce7b18ae3e00 Mon Sep 17 00:00:00 2001 From: Andy Baker Date: Sat, 14 Dec 2024 11:08:31 +0000 Subject: [PATCH] Experimental Android specific ram logging --- Assets/Scripts/desktop_app/DebugConsole.cs | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Assets/Scripts/desktop_app/DebugConsole.cs b/Assets/Scripts/desktop_app/DebugConsole.cs index 54514345..4dd03537 100644 --- a/Assets/Scripts/desktop_app/DebugConsole.cs +++ b/Assets/Scripts/desktop_app/DebugConsole.cs @@ -167,6 +167,9 @@ private void RunCommand(string command) case "publish": CommandPublish(parts); break; + case "ram": + CommandLogRam(parts); + break; case "rest": CommandRest(parts); break; @@ -197,6 +200,44 @@ private void PrintLn(string message) consoleOutput.text += message + "\n"; } + private void CommandLogRam(string[] parts) + { + if (Application.platform == RuntimePlatform.Android) + { + using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + using (var activity = unityPlayer.GetStatic("currentActivity")) + using (var activityManager = activity.Call("getSystemService", "activity")) + using (var memoryInfo = new AndroidJavaObject("android.app.ActivityManager$MemoryInfo")) + { + activityManager.Call("getMemoryInfo", memoryInfo); + + long availMem = memoryInfo.Get("availMem"); + long totalMem = memoryInfo.Get("totalMem"); + long threshold = memoryInfo.Get("threshold"); + + long usedMem = totalMem - availMem; + + PrintLn($"Total Memory: {totalMem / (1024.0 * 1024.0):F2} MB"); + PrintLn($"Available Memory: {availMem / (1024.0 * 1024.0):F2} MB"); + PrintLn($"Used Memory: {usedMem / (1024.0 * 1024.0):F2} MB"); + PrintLn($"Low Memory Threshold: {threshold / (1024.0 * 1024.0):F2} MB"); + + // Compare app memory usage + long appMemoryUsage = System.GC.GetTotalMemory(false); + PrintLn($"App Memory Usage: {appMemoryUsage / (1024.0 * 1024.0):F2} MB"); + + if (availMem < threshold) + { + PrintLn("Warning. Device is running low on memory. App may be terminated soon."); + } + } + } + else + { + PrintLn("This feature is available only on Android."); + } + } + private void CommandOsQ(string[] parts) { if (parts.Length != 2)