views:

759

answers:

3

Is the a VS2005 C++ compiler flag like the Xmx???M java flag so I can limit the heap size of my application running on Windows.

I need to limit the heap size so I can fill the memory to find out the current free memory. (The code also runs on an embedded system where this is the best method to get the memory usage)

A: 

You might want to look into whether the gflags utility (in the Windows Debugging Tools) can do this. It can do a lot of other interesting things with the heap of native applications.

Lou Franco
A: 

The heap size depends on the allocator used. There might also be some Windows API call that limits the amount of memory a process can allocate, but I'm not aware of one and I don't feel like looking for it right now, sorry. But in general, if you write your own allocator (maybe just wrap around the compiler-provided malloc() or new operator) you can artificially limit the heap size that way.

Alternatively, if you have your own allocator, even if just a wrapper, you can keep track of how much memory has been allocated in total. If you know the amount available you can just do some subtraction and be done with getting the total. You might also be able to get fragmentation statistics then, like largest free block.

jfs
the problem with replacing the allocator to limit heap or keep track of memory usage is that only works where you can actually make that replacing... cant really limit heap or keep track of allocations made by 3rd party libs and OS modules
mrlinx
+1  A: 

You can set the heap size for your program by setting the size in:

Linker -> System -> Heap Reserve Size

It can also be set at the compiler command line using /HEAP:reserve

Ashwin