RAM usage : check the available amount, increase it

Hello,

I’m recording the webcam images stream in RAM, by creating a deque< ofImage >
( I do setUseTexture(false) for each ofImage. )
I want to prevent the app to crash when there is not enough avalaible RAM.

My first idea is to check the available RAM amount. I’m working with windows 7. I tried:

MEMORYSTATUSEX status ;
status.dwLength = sizeof( status ) ;
GlobalMemoryStatusEx( &status ) ;
cout << "status.ullTotalPhys :" << status.ullTotalPhys << endl;
cout << "status.ullAvailPhys :" << status.ullAvailPhys << endl;
cout << "status.ullTotalPageFile :" << status.ullTotalPageFile << endl;
cout << "status.ullAvailPageFile :" << status.ullAvailPageFile << endl;
cout << "status.ullTotalVirtual :" << status.ullTotalVirtual << endl;
cout << "status.ullAvailVirtual :" << status.ullAvailVirtual << endl << endl;

I get theses values when I start the recording:

status.ullTotalPhys : 8 296 919 040           // Seems OK: I've got a 8GB RAM
status.ullAvailPhys : 4 222 210 048           // Seems OK according to my system monitor
status.ullTotalPageFile : 16 591 933 440      // Seems OK: I've got a 8GB swap
status.ullAvailPageFile :  12 091 158 528     // Seems OK too
status.ullTotalVirtual : 2 147 352 576        // Not sure about that. Perhaps the max RAM dedicated to my app ?
status.ullAvailVirtual : 1 836 437 504        // Not sure about that. Perhaps the remaining RAM available to my app ?

I get theses values when the program crash:

status.ullTotalPhys : 8 296 919 040
status.ullAvailPhys : 2 551 107 584
status.ullTotalPageFile : 16 591 933 440
status.ullAvailPageFile : 10 422 988 800
status.ullTotalVirtual : 2 147 352 576
status.ullAvailVirtual : 168 988 672

None of theses values are close to zero. ullAvailVirtual is the smallest one, perhaps the one that tell me how many RAM is still available for my app, but 168 988 672 bytes is enough to store many frames (a 1920x1080 color image takes 6 220 800 bytes).

Question 1: Does anyone can help me to understand ? Should I stop the recording when ullAvailVirtual is less than 200 000 000 bytes ?

When the app crash my system monitor tell me that it use 1 809 336 KB of “private bytes”. Question 2: I wondering if there is a way to allow the app to use more RAM. Is it some kind of 2GB limitation for a 32 bit program ?

Another test I can do is:

void * test = malloc( size_t( cam->getWidth() * cam->getHeight() ) * 3 + 100000000 );
if( test ){
    free( test );
    // store the image
}

( I add 100 000 000 to the malloc call to reserve 100MB for the rest of the app lifetime)
Question 3: This solution work, but what do you think about that ? Isn’t it a litte bit dirty or slow ?

Thank you !

P.S: I know there are some solutions to save RAM space: downscale the frames, limit the fps, or record to a video file. But I want to understand what is happening here.