Detecting when memory exhaustion occurs (retrieving the sum of "free physical memory")

I transfer images from a camera with a high FPS to the memory buffer (list), and since these images are quite large, the computer runs out pretty quickly.

What I would like to do is stop transferring for a while before the app runs out of memory. During my testing, I found that it corresponds to the indicator "Free physical memory", approaching zero.

Now the problem is that I cannot find a way to get this value programmatically; in XP, it does not even appear anywhere (only in the Vista / 7 task manager).

alt text

I tried everything I could find (WMI, performance counters, MemoryStatus, ...), but everything I got from them was just "Available physical memory", which, of course, is not the same.

Any ideas?

Update Unfortunately, I need data in memory (yes, I know that I can not guarantee that it will be in physical memory, but still), because the data is transmitted in real time, and I need to view it in memory after it there have been saved.

+3
source share
6 answers

, , System.Runtime.MemoryFailPoint? , , , InsufficientMemoryException, ; . , 3 4 , . , - ?

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 enough memory for at least a few
    // more frames
}

, 100% , . , , .

+6

. " " . ; , , .

"" , . , , . - .

, . - , . , , , .

. , .

+8

Vista/7 , . Vista/7, , , , , , .

Linky: http://www.codinghorror.com/blog/2006/09/why-does-vista-use-all-my-memory.html

, 32- #, 2 ( 1,5 , ), , , , 2 .

Tergiver , , .

+2

.

, ( ) , , - , .

, , ( - ), , , , , ( ).

, , . , , .

+1

, OwenP , System.Runtime.MemoryFailPoint.

: public MemoryFailPoint(int sizeInMegabytes), AverageFrameSize , . :

MemoryFailPoint 16 . 16 16 , 16 .

, MemoryFailPoint , , , !

, , OP.

, , , MemoryFailPoint , . , , , . ( MemoryFailPoint !)

"" Dispose(). , MemoryFailPoint -instance , , . ( " " , . , .)

. , MemoryFailPoint , . ( - , MSDN.)

( ) :

const int FrameSizeInMegabytes = 10; // 10MB (perhaps more is needed?)
const int FrameSizeInBytes = FrameSizeInMegabytes << 20;
// shifting by 20 is the same as multiplying with 1024 * 1024.

bool TryCreateImageBuffer(int numberOfImages, out byte[,] imageBuffer)
{
    // check that it is theoretically possible to allocate the array.
    if (numberOfImages  < 0 || numberOfImages > 0x7FFFFFC7)
        throw new ArgumentOutOfRangeException("numberOfImages",
            "Outside allowed range: 0 <= numberOfImages <= 0x7FFFFFC7");

    // check that we have enough memory to allocate the array.
    MemoryFailPoint memoryReservation = null;
    try
    {
        memoryReservation =
            new MemoryFailPoint(FrameSizeInMegabytes * numberOfImages);
    }
    catch (InsufficientMemoryException ex)
    {
        imageBuffer = null;
        return false;
    }

    // if you get here, there likely to be enough memory
    // available to create the buffer. Normally we can't be
    // 100% sure because another thread might allocate memory
    // without first reserving it with MemoryFailPoint in
    // which case you have a race condition for the allocate.
    // Because of this the allocation should be done as soon
    // as possible - the longer we wait the higher the risk.
    imageBuffer = new byte[numberOfImages, FrameSizeInBytes];

    //Now that we have allocated the memory we can go ahead and call dispose
    memoryReservation.Dispose();

    return true;
}

0x7FFFFFC7 - , , MSDN .

( MemoryFailPoint) :

const int AverageFrameSizeInMegabytes = 10; // 10MB

/// <summary>
/// Tries to create a MemoryFailPoint instance for enough megabytes to
/// hold as many images as specified by <paramref name="numberOfImages"/>.
/// </summary>
/// <returns>
/// A MemoryFailPoint instance if the requested amount of memory was
/// available (at the time of this call), otherwise null.
/// </returns>
MemoryFailPoint GetMemoryFailPointFor(int numberOfImages)
{
    MemoryFailPoint memoryReservation = null;
    try
    {
        memoryReservation =
            new MemoryFailPoint(AverageFrameSizeInMegabytes * numberOfImages);
    }
    catch (InsufficientMemoryException ex)
    {
        return null;
    }
    return memoryReservation;
}

( ), MemoryFailPoint . ( ​​ , .)

: ""

"" , ( ). , MemoryFailPoint , , , ( "" ) , MemoryFailPoint. MemoryFailPoint (, ), () "" . ( MemoryFailPoint, .). "", , MemoryFailPoint .

, "" . , , , :

// Note that multiple threads can still ---- on our free chunk of address space, which can't be easily solved.

, .


2GB .

, , <gcAllowVeryLargeObjects>, , .


, , OP. , MemoryFailPoint , , - -. , OutOfMemoryException, , , OP.

, , , AllocateUserPhysicalPages, , , . OS , , , ...

+1

OutOfMemoryException , . , . , 2 . 32- , , , , .

OutOfMemoryExceptions . , , . , OOM .

0

Source: https://habr.com/ru/post/1781016/


All Articles