unity System.Diagnostics.Process类来获取Unity程序的内存使用情况,一直返回0
时间: 2024-02-23 13:02:50 浏览: 99
显示进程的内存使用情况
如果你使用 System.Diagnostics.Process 类来获取 Unity 程序的内存使用情况,一直返回 0 的话,可能是因为 Unity 程序是以 Mono 运行时的方式来启动的。在这种情况下,你需要使用 Mono 提供的方法来获取内存使用情况。你可以使用下面的代码来获取 Unity 程序的内存使用情况:
```
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class MemoryUsage
{
[DllImport("libc")]
static extern int getpid();
[DllImport("libproc")]
static extern int proc_pid_rusage(int pid, int flavor, out RUsageInfo info);
[StructLayout(LayoutKind.Sequential)]
struct RUsageInfo
{
public long user_time;
public long system_time;
public long max_resident_size;
public long shared_text_size;
public long unshared_data_size;
public long unshared_stack_size;
public long page_reclaims;
public long page_faults;
public long swaps;
public long block_input_ops;
public long block_output_ops;
public long messages_sent;
public long messages_received;
public long signals_received;
public long voluntary_context_switches;
public long involuntary_context_switches;
}
public static long GetMemoryUsage()
{
int pid = getpid();
RUsageInfo info;
int ret = proc_pid_rusage(pid, 0, out info);
if (ret != 0)
{
throw new Exception("Failed to get process resource usage");
}
return info.max_resident_size;
}
}
```
然后在你的代码中调用 `MemoryUsage.GetMemoryUsage()` 就可以获取 Unity 程序的内存使用情况了。
阅读全文