Files
Studio/Assets/Scripts/ExternalAssets/TriLib/TriLibSamples/AssetViewer/Utils/RuntimeProcessUtils.cs
2025-06-11 16:50:56 +09:00

44 lines
1.4 KiB
C#

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TriLibCore.Samples
{
public class RuntimeProcessUtils
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
[DllImport("psapi.dll", SetLastError = true)]
private static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, int size);
[StructLayout(LayoutKind.Sequential, Size = 72)]
private struct PROCESS_MEMORY_COUNTERS
{
public UInt32 cb;
public UInt32 PageFaultCount;
public UInt64 PeakWorkingSetSize;
public UInt64 WorkingSetSize;
public UInt64 QuotaPeakPagedPoolUsage;
public UInt64 QuotaPagedPoolUsage;
public UInt64 QuotaPeakNonPagedPoolUsage;
public UInt64 QuotaNonPagedPoolUsage;
public UInt64 PagefileUsage;
public UInt64 PeakPagefileUsage;
}
public static long GetProcessMemory()
{
if (GetProcessMemoryInfo(Process.GetCurrentProcess().Handle, out var processMemoryCounters, Marshal.SizeOf<PROCESS_MEMORY_COUNTERS>()))
{
var memoryUsage = processMemoryCounters.WorkingSetSize;
return (long)memoryUsage;
}
return 0;
}
#else
public static long GetProcessMemory()
{
return 0;
}
#endif
}
}