How to find free physical memory

Hi all,

I am using new operator to allocate the memory
and I want to use maximum free memory for my
program without deleting previously allocated memory.

After allocating some memory new throws
bad_alloc exception for insufficient memory
now I want to avoid this exception.

So before allocating memory I have to check amount
of free memory. I am using GlobalMemoryStatus to get
the current free memory.

new is throwing exception still GlobalMemoryStatus shows
sufficient free memory which is more than I am trying to allocate.

I am using Visual Studio 2005 for development .

Is there any other way to check correct free memory..
Please help me..
Catch the exception or use set_new_handler (or whatever it is called).
The computer probably won't let your program use ALL of the memory on the system, so you are stuck. There is no way to "force" the computer to give you memory when it doesn't have any (or doesn't want) to give.
Is there any other way to check the memory other than GlobalMemoryStatus()...
Are you trying to allocate with a single call to new[]?

new[] requires the memory you allocate be contiguous. The total available memory might be all fragmented to hell.
no I am calling new number of times like following..
1
2
3
4
5
6
7
8
9

int *pI;
unsigned long BIG_NUMBER = 100000000;

while(1)
{
pI = new int[BIG_NUMBER];
}
You man need to directly to the OS with VirtualAlloc. But before you do, think about what you mean by maximum free memory. This is not a fixed measure in a multi-processor/multi-user/virtual memory system, and it may not mean what you think it means.
Last edited on
Topic archived. No new replies allowed.