检测何时将要用完内存(获取“可用物理内存"的数量) [英] Detecting when about to run out of memory (getting the amount of "free physical memory")

查看:139
本文介绍了检测何时将要用完内存(获取“可用物理内存"的数量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像从高FPS相机传输到内存缓冲区(列表)中,并且由于这些图像很大,因此计算机很快就会耗尽内存.

我想做的是在应用程序内存不足之前 停止传输.在测试过程中,我发现它与可用物理内存"指示器接近零是一致的.

现在的问题是,我找不到真正以编程方式获取此值的方法;在XP中,它甚至都不会显示在任何地方(仅在Vista/7任务管理器中).

我已经尝试了所有可以找到的方法(WMI,性能计数器,MemoryStatus等),但是从这些方法中得到的一切仅仅是可用物理内存",当然并不相同. >

有什么想法吗?

更新 不幸的是,我需要将数据存储在内存中(是的,我知道我不能保证将其存储在物理内存中,但是仍然可以),因为数据是实时流式传输的,我需要将其存储在内存中后预览.

解决方案

我参加聚会很晚,但是您是否考虑过使用

Now the problem is that I can't find a way actually to get this value programmatically; in XP, it is not even displayed anywhere (just in the Vista/7 task manager).

I have tried all the ways I could find (WMI, performance counters, MemoryStatus, ...), but everything I got from those was just the "Available Physical Memory," which is of course not the same.

Any ideas?

Update Unfortunately, I need the data to be in memory (yes, I know I can't guarantee it will be in physical memory, but still), because the data is streamed in real-time and I need to preview it in memory after it's been stored there.

解决方案

I'm late to the party, but have you considered using the System.Runtime.MemoryFailPoint class? It does a bunch of stuff to ensure that the requested allocation would succeed and throws InsufficientMemoryException if it fails; you can catch this and stop your transfer. You can probably predict an average size of incoming frames and try to allocate 3 or 4 of them, then stop acquisition when a failure is made. Maybe something like this?

const int AverageFrameSize = 10 * 1024 * 1024 // 10MB

void Image_OnAcquired(...)
{
    try
    {
        var memoryCheck = new MemoryFailPoint(AverageFrameSize * 3);
    }
    catch (InsufficientMemoryException ex)
    {
        _camera.StopAcquisition();
        StartWaitingForFreeMemory();
        return;
    }

    // if you get here, there's enough memory for at least a few
    // more frames
}

I doubt it'd be 100% foolproof, but it's a start. It's definitely more reliable than the performance counter for reasons that are explained in the other answers.

这篇关于检测何时将要用完内存(获取“可用物理内存"的数量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆