Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
windows c#内存不足通知_C#_Memory Management_Out Of Memory - Fatal编程技术网

windows c#内存不足通知

windows c#内存不足通知,c#,memory-management,out-of-memory,C#,Memory Management,Out Of Memory,如何从c#订阅Windows内存不足通知 我们的c#应用程序有大量非托管内存分配,如果操作系统内存可用性低,我们可以释放这些内存。使用CreateMorymoryResourceNotification和QueryMemoryResourceNotification检查内存状态 enum MemoryResourceNotificationType : int { LowMemoryResourceNotification = 0, HighMem

如何从c#订阅Windows内存不足通知


我们的c#应用程序有大量非托管内存分配,如果操作系统内存可用性低,我们可以释放这些内存。

使用CreateMorymoryResourceNotification和QueryMemoryResourceNotification检查内存状态

    enum MemoryResourceNotificationType : int
    {
        LowMemoryResourceNotification = 0,
        HighMemoryResourceNotification = 1,
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateMemoryResourceNotification(MemoryResourceNotificationType notificationType);

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool QueryMemoryResourceNotification(IntPtr resourceNotificationHandle, out int resourceState);

    private static IntPtr MemoryResourceNotificationHandle;

    public static void TryReclaim()
    {
        MemoryResourceNotificationHandle = CreateMemoryResourceNotification(MemoryResourceNotificationType.LowMemoryResourceNotification);

        int sleepIntervalInMs = ReclaimIntervalInSeconds * 1000;

        while (true)
        {
            Thread.Sleep(10_000);

            bool isSuccecced = QueryMemoryResourceNotification(MemoryResourceNotificationHandle, out int memoryStatus);

            if (isSuccecced)
            {
                if (memoryStatus >= 1)
                {
                   DoReclaim();
                }

            }

        }           


    }

你可以在价格低的时候做点什么。是的,我们现在就这样做,每60年代循环一次。但低内存通知肯定是理想的吗?在
SystemEvents
中有一个名为
LowMemory
的事件,但据我所知,它已经过时了。@Johnny query WMI?WMI是否有办法将内存不足事件“推送”到应用程序?@Johnny决定通过PInvoke使用QueryMemoryResourceNotification和CreateMorymoryResourceNotification订阅windows内存不足事件