获取的是应用在移动设备上面的实时允许是的进程使用内存大小和你直接在移动设备上查看进程的内存使用一模一样,绝对精确
C#
public static int GetUseMemory()
{
int memory = -1;
#if UNITY_ANDROID
try
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名");
float tempMemory = unityPluginLoader.CallStatic<float>("GetMemory", currentActivity);
memory = (int)tempMemory;
}
catch (System.Exception e)
{
ILog.Info("com.moba.unityplugin.AndroidUtile GetMemory: " + e.Message);
}
#elif UNITY_IOS
memory = (int)_IOS_GetTaskUsedMemeory();
#endif
return memory;
}
Android
public static float GetMemory(Activity currentActivity) {
float memory = -1;
try {
int pid = android.os.Process.myPid();
ActivityManager mActivityManager = (ActivityManager) currentActivity
.getSystemService(Context.ACTIVITY_SERVICE);
Debug.MemoryInfo[] memoryInfoArray = mActivityManager.getProcessMemoryInfo(new int[] { pid });
memory = (float) memoryInfoArray[0].getTotalPrivateDirty() / 1024;
} catch (Exception e) {
if (Utile.isDebug())
Utile.LogError(e.toString());
}
return memory;
}
Ios
long _IOS_GetTaskUsedMemeory()
{
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&taskInfo,
&infoCount);
if (kernReturn == KERN_SUCCESS) {
long usedMemory = taskInfo.resident_size/1024.0/1024.0;
return usedMemory;
}
return 0;
}
网友评论