Hi community,
I’m having an issue with this method on Samsung s9+.
I have tested on two of different s9+. And they returned two different value.
The result:
Android Model: Samsung SM-G965U1, Ram 5009 MB
Android Model: Samsung SM-G965F, Ram 1099 MB
Tested on Unity: 5.6.4p4, IL2CPP
Any advice, please.
Hi! We’ve tested this on a large number of devices and have come up with code changes that landed in 2018.3, but feel that the risk of a regression is too high to backport, considering that there were only two problematic devices (including the SM-G965F).
However, you could use the following script to get the total and available Memory from the native ActivityManager.getMemoryInfo call:
const uint kMegabyte = 1048576u;
AndroidJavaObject getMemoryInfo()
{
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject systemService = currentActivity.Call<AndroidJavaObject>("getSystemService", "activity");
AndroidJavaObject memoryInfo = new AndroidJavaObject("android.app.ActivityManager$MemoryInfo");
systemService.Call("getMemoryInfo", memoryInfo);
return memoryInfo;
}
int getAvailMemory()
{
using (var memInfo = getMemoryInfo())
{
return (int)(memInfo.Get<long>("availMem") / kMegabyte);
}
}
int getTotalMemory()
{
using (var memInfo = getMemoryInfo())
{
return (int)(memInfo.Get<long>("totalMem") / kMegabyte);
}
}