How can I calculate the amount of free virtual address space my process have on Windows?
如何计算我的进程在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.
更重要的是:这需要按流程计算,因为我只关心我的流程,我仍在使用Win32。
I have tried VirtualQuery(), but it is not returning the total address space remaining.
我尝试过VirtualQuery(),但它没有返回剩余的总地址空间。
Thank you.
3 个解决方案
#1
Might want to look into GetProcessMemoryInfo or GetProcessWorkingSetSizeEx to determine how much memory you are using.
可能需要查看GetProcessMemoryInfo或GetProcessWorkingSetSizeEx来确定您使用的内存量。
#2
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.
使用Win32 API中的GlobalMemoryStatusEx函数。您感兴趣的字段是ullAvailVirtual,它是您的进程尚未保留或提交的虚拟空间的字节数。
#3
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.
如果您使用的是c ++,则可以使用自定义内存分配器。但是,您可能不需要跟踪每个内存分配,只需要程序的大多数内存密集型部分。
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.
一种选择是在启动时分配内存块,并使用LRU缓存重用已分配的内存块,并仅将最近使用的数据保留在内存中。您的程序也会更快(在运行时不需要分配),您将防止因分配/释放内存而导致的内存碎片。
#1
Might want to look into GetProcessMemoryInfo or GetProcessWorkingSetSizeEx to determine how much memory you are using.
可能需要查看GetProcessMemoryInfo或GetProcessWorkingSetSizeEx来确定您使用的内存量。
#2
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.
使用Win32 API中的GlobalMemoryStatusEx函数。您感兴趣的字段是ullAvailVirtual,它是您的进程尚未保留或提交的虚拟空间的字节数。
#3
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.
如果您使用的是c ++,则可以使用自定义内存分配器。但是,您可能不需要跟踪每个内存分配,只需要程序的大多数内存密集型部分。
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.
一种选择是在启动时分配内存块,并使用LRU缓存重用已分配的内存块,并仅将最近使用的数据保留在内存中。您的程序也会更快(在运行时不需要分配),您将防止因分配/释放内存而导致的内存碎片。