Virtual Size and GlobalMemoryStatusEx
My system is Windows XP. Virtual Size displayed in TaskManager is different with MEMORYSTATUSEX.ullAvailVirtual got from GlobalMemoryStatusEx.
When I create lot of buffers and the memory usage is up, MEMORYSTATUSEX.ullAvailVirtual can well reflected the virtual size usage. It's same.
But when I delete the memory, Virtual Size in task manager is down, but MEMORYSTATUSEX.ullAvailVirtual is still very small. I开发者_StackOverflow don't know why....
I am totally confused.
You could be suffering from memory fragmentation. (I.e. if you lea a few bytes between each large allocation, it effectively forces up the virtual bytes of your application).
You might find it more reliable to compare figures against perfmon - the counters I've always used in the past have been Private bytes (memory actually allocated) and Virtual bytes (memory address space allocated) - if those two counters diverge, then you have a memory fragmentation problem, which will be the result of a memory leak. The figures in Task Manager, whilst true and accurate, don't convey anything particularly useful.
When you delete allocated memory, the OS doesn't immediately return that memory but keeps it reserved for the process, at least until another process needs that memory. This improves the performance, because the very same process might need the just deleted memory a few ms later again.
To really free the deleted memory, you can call
SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
Maybe that will force GlobalMemoryStatusEx() to return the values you expect?
精彩评论