After parsing MIDI, CSharp wants to write a memory occupation display. Generally, when writing CPP programs, it likes to check the memory and prevent sand carving operations such as memory explosion.
After searching, the general way of writing CSharp is
Process CurrentProcess = Process.GetCurrentProcess(); CurrentProcess.WorkingSet64
It's a lot easier than CPP. It's tested successfully under XNA.
The test return value under Unity is always 0, so is google.
No one knows why, but it's mentioned that there seems to be plug-ins in the plug-in market that can display process memory in Unity.
I simply thought that maybe I called the Win32 API.
Turn out the previous CPP DLL test project, and simply encapsulate the code of CPP calling process and memory.
#include <windows.h> #include <psapi.h> #ifdef __cplusplus // If used by C++ code, extern "C" { // we need to export the C interface #endif int memoryUsage; DWORD processID; HANDLE hProcess; PROCESS_MEMORY_COUNTERS pmc; __declspec(dllexport) void InitMemInfo() { processID = GetCurrentProcessId(); if (processID) { hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); } else { memoryUsage = -1; } } __declspec(dllexport) void DestroyMemInfo() { if (hProcess) CloseHandle(hProcess); } __declspec(dllexport) int Frame() { if (hProcess) { if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) { memoryUsage = pmc.WorkingSetSize; } } return memoryUsage; } #ifdef __cplusplus } #endif
In Unity, simply call
using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.Profiling; public class MemoryManager : MonoBehaviour { public string memInfo = ""; [DllImport("MemInfo")] public static extern void InitMemInfo(); [DllImport("MemInfo")] public static extern void DestroyMemInfo(); [DllImport("MemInfo")] public static extern int Frame(); private double inv10241024 = 0.00000095367431640625; void Start() { InitMemInfo(); } void OnDestroy() { DestroyMemInfo(); } void Update() { float memoryUsed = (float)(Frame() * inv10241024); memInfo = memoryUsed.ToString("0.000") + " MB"; } private void OnGUI() { GUI.Label(new Rect(0, 0, 400, 50), memInfo); } }
To solve this problem, there is a kind of pain of taking off your pants and farting, but it seems that only in this way can the process memory set be displayed. It's estimated that it's OK to directly import a system dll and call Win32 API functions directly. If you're lazy and don't experiment, it's OK to verify the feasibility. Even if the time goes too fast, you'll go to bed at 3:30 a.m. in a mess