Skip to content

Commit

Permalink
Experimental Android specific ram logging
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Dec 14, 2024
1 parent 631da7b commit c6d0035
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Assets/Scripts/desktop_app/DebugConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ private void RunCommand(string command)
case "publish":
CommandPublish(parts);
break;
case "ram":
CommandLogRam(parts);
break;
case "rest":
CommandRest(parts);
break;
Expand Down Expand Up @@ -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<AndroidJavaObject>("currentActivity"))
using (var activityManager = activity.Call<AndroidJavaObject>("getSystemService", "activity"))
using (var memoryInfo = new AndroidJavaObject("android.app.ActivityManager$MemoryInfo"))
{
activityManager.Call("getMemoryInfo", memoryInfo);

long availMem = memoryInfo.Get<long>("availMem");
long totalMem = memoryInfo.Get<long>("totalMem");
long threshold = memoryInfo.Get<long>("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)
Expand Down

0 comments on commit c6d0035

Please sign in to comment.