Xamarin.iOSでInstrumentsを使わずにアプリの使用メモリ量を取得する
アプリの使っているメモリ量はInstrumentsを使うと詳しく調査できるのだが、
パフォーマンスが悪かったりコードの特定の地点での値を得るのが難しい。
こういう場合本家ではtask_info()を使用してtask_basic_info構造体を取得すればよいのだが、Xamarin.iOSではまだバイディングされていないようなので自前でバイディングしてみた。
using System; using System.Runtime.InteropServices; static class Diag { public const int TASK_BASIC_INFO = 4; public struct TimeValue { public int Seconds; public int Microseconds; } public struct TaskBasicInfo { public int SuspendCount; public uint VirtualSize; public uint ResidentSize; public TimeValue UserTime; public TimeValue SystemTime; public int Policy; } [DllImport("/usr/lib/system/libsystem_kernel.dylib", CallingConvention = CallingConvention.Cdecl)] public static extern uint mach_task_self(); [DllImport("/usr/lib/system/libsystem_kernel.dylib", CallingConvention = CallingConvention.Cdecl)] public static extern int task_info(uint targetTaskID, int flavor, ref TaskBasicInfo taskInfo, ref int size); public static TaskBasicInfo GetTaskInfo() { TaskBasicInfo taskInfo = new TaskBasicInfo(); uint taskid = mach_task_self(); int size = Marshal.SizeOf(typeof(TaskBasicInfo)); task_info(taskid, TASK_BASIC_INFO, ref taskInfo, ref size); return taskInfo; } }
※このコードではTaskBasicInfo.UserTimeとTaskBasicInfo.SystemTimeが取得できない。おそらくどこか間違っているんだろう。