tags:

views:

189

answers:

3

How can I calculate the amount of free virtual address space my process have on Windows?

My application need to limit the amount of address space used. So I need to estimate how many memory I have consumed and how many virtual memory is left. If I have just a few hundred megabytes of address space left, my process begins to use a custom paging system to avoid loading to much data in memory.

And more important: this needs to be calculated per process, because I only care about my process, I am still using Win32.

I have tried VirtualQuery(), but it is not returning the total address space remaining.

Thank you.

+1  A: 

Might want to look into GetProcessMemoryInfo or GetProcessWorkingSetSizeEx to determine how much memory you are using.

Eric Petroelje
A: 

If you're using c++ you can use custom memory allocators. But probably you don't need to track every memory allocation, only most memory intensive parts of your program need to.

One option is allocate blocks of memory at start-up and use a LRU cache to reuse your allocated memory blocks and keep in memory only most recently used data. Your program will also be faster (no allocation is needed during runtime) and you will prevent memory fragmentation due to allocating/freeing memory.

davidnr
A: 

Use GlobalMemoryStatusEx function from Win32 API. The field you are interested in is ullAvailVirtual, which is the number of bytes of virtual space that has not been reserved or committed by your process.