Memory Limit in Unmanged C++

L

Leon Lambert

How do i get the amount of memory left for an application to allocate?
We have an application that badly needs redesign because the current
design can under certain circumstances run out of memory. Until we can
redesign it we need to add protections to prevent a crash. I would like
to determine when we are close to running out of memory and do a clean
abort of the current operation.

Using PSApi i can use GetProcessMemoryInfo to get the current
WorkingSetSize. I can't seem to find a number i can compare this against
to calculate approximate memory left. I know there is a hard 2 gig limit
but i think this can be lower based on pagefile sizes and such so would
like to make a Win32 call to get the limit.

Any help on how to figure out how much memory is left for a process to
use would be greatly appreciated.

Leon Lambert
 
L

Larry Smith

How do i get the amount of memory left for an application to allocate? We
have an application that badly needs redesign because the current design
can under certain circumstances run out of memory. Until we can redesign
it we need to add protections to prevent a crash. I would like to
determine when we are close to running out of memory and do a clean abort
of the current operation.

Using PSApi i can use GetProcessMemoryInfo to get the current
WorkingSetSize. I can't seem to find a number i can compare this against
to calculate approximate memory left. I know there is a hard 2 gig limit
but i think this can be lower based on pagefile sizes and such so would
like to make a Win32 call to get the limit.

Any help on how to figure out how much memory is left for a process to use
would be greatly appreciated.

http://msdn2.microsoft.com/en-us/library/aa366589.aspx
http://msdn2.microsoft.com/en-us/library/aa366781.aspx
 
B

Ben Voigt [C++ MVP]

Leon Lambert said:
How do i get the amount of memory left for an application to allocate? We
have an application that badly needs redesign because the current design
can under certain circumstances run out of memory. Until we can redesign
it we need to add protections to prevent a crash. I would like to
determine when we are close to running out of memory and do a clean abort
of the current operation.

Using PSApi i can use GetProcessMemoryInfo to get the current
WorkingSetSize. I can't seem to find a number i can compare this against
to calculate approximate memory left. I know there is a hard 2 gig limit
but i think this can be lower based on pagefile sizes and such so would
like to make a Win32 call to get the limit.

Any help on how to figure out how much memory is left for a process to use
would be greatly appreciated.

Realize that "bytes free" is nowhere near as important as the number and
sizes of free fragments on the heap.

If possible, can you allocate a decent-sized buffer for each operation, use
pieces of that buffer for the various temporary variables the operation
needs, and then free the whole thing when the operation finishes? Then you
will decrease fragmentation a lot, and also there's only one allocation that
can fail.
 
L

Leon Lambert

Ben said:
Realize that "bytes free" is nowhere near as important as the number and
sizes of free fragments on the heap.

If possible, can you allocate a decent-sized buffer for each operation, use
pieces of that buffer for the various temporary variables the operation
needs, and then free the whole thing when the operation finishes? Then you
will decrease fragmentation a lot, and also there's only one allocation that
can fail.
I am not trying to get to be able to allocate the last bit of memory
available. I want a gross way of determining when a particular operation
is going crazy allocating memory. For example let say that 2 gig is the
maximum allocatable memory in the process. I would like to know when an
operation is getting within 300 or 400 megabytes of that maximum. This
leaves me plenty of space to do a clean abort. It would seem that link
that Larry provided will yield the information I need. Thanks for taking
the time to respond.

Leon Lambert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top