Unity 获取 不同平台上内存的使用情况 总结

1.PC平台

 [StructLayout(LayoutKind.Sequential,Pack =1)]
    public struct MEMORYSTATUSEX
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        //系统内存总量
        public ulong ullTotalPhys;
        //系统可用内存
        public ulong ullAvailPhys;
        public ulong ullTotalPageFile;
        public ulong ullAvailPageFile;
        public ulong ullTotalVirtual;
        public ulong ullAvailVirtual;
        public ulong ullAvailExtendedVirtual;
    }

    //extern  调用 其他平台 dll 关键字
    [DllImport("kernel32.dll")]
    protected static extern void GlobalMemoryStatus(ref MEMORYSTATUSEX lpBuff);
   

    /// <summary>
    /// 获取可用内存
    /// PC
    /// </summary>
    /// <returns></returns>
    protected ulong GetWinAvailMemory()
    {
        MEMORYSTATUSEX ms = new MEMORYSTATUSEX();
        ms.dwLength = 64;
        GlobalMemoryStatus(ref ms);
        return ms.ullAvailPhys;
    }

    /// <summary>
    /// 获取总内存
    /// PC
    /// </summary>
    /// <returns></returns>
    protected ulong GetWinTotalMemory()
    {
        MEMORYSTATUSEX ms = new MEMORYSTATUSEX();
        ms.dwLength = 64;
        GlobalMemoryStatus(ref ms);
        return ms.ullTotalPhys;
    }

    /// <summary>
    /// 返回应用使用的内存
    /// PC
    /// </summary>
    /// <returns></returns>
    protected long GetWinUsedMemory()
    {
        return UnityEngine.Profiling.Profiler.GetTotalReservedMemoryLong();
    }

内存快爆掉了。。。。

2.Android端

 //当前系统可用内存
  ActivityManager am = 
                (ActivityManager)m_Activity.getSystemService(Context.ACTIVITY_SERVICE);
  MemoryInfo mi= new MemoryInfo();
  am.getMemoryInfo(mi);
  return mi.availMem;

  //当前系统总内存
   return  GetTotalMemory();
   //应用所占内存
    Debug.MemoryInfo memoryInfo=new Debug.MemoryInfo();
    Debug.getMemoryInfo(memoryInfo);
    return  memoryInfo.getTotalPss()*1024;

3.IOS 

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class IOSMemoryUsage : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern int task_info(int target_task, int task_info_flavor, ref IntPtr task_info_out, ref uint task_info_outCnt);

    [DllImport("__Internal")]
    private static extern int mach_task_self();

    private const int TASK_VM_INFO = 22; // 根据实际需求可以修改task_info_flavor的值

    private void Start()
    {
        long memoryUsage = GetMemoryUsage();
        Debug.Log("IOS Memory Usage: " + memoryUsage + " bytes");
    }

    private long GetMemoryUsage()
    {
        IntPtr taskInfoPtr = IntPtr.Zero;
        uint taskInfoSize = 0;
        task_info(mach_task_self(), TASK_VM_INFO, ref taskInfoPtr, ref taskInfoSize);

        TASK_VM_INFO taskInfo = (TASK_VM_INFO)Marshal.PtrToStructure(taskInfoPtr, typeof(TASK_VM_INFO));
        Marshal.FreeHGlobal(taskInfoPtr);

        return taskInfo.phys_footprint;
    }
}

[StructLayout(LayoutKind.Sequential)]
public struct TASK_VM_INFO
{
    public uint virtual_size;
    public uint resident_size;
    public uint resident_size_max;
    public uint shared_clean;
    public uint private_clean;
    public uint shared_dirty;
    public uint private_dirty;
    public uint phys_footprint;
    public uint min_address;
    public uint max_address;
}


           
             
           
             

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值