开发者

How to show percentage of 'memory used' in a win32 process?

开发者 https://www.devze.com 2022-12-24 05:27 出处:网络
I know that memory usage is a very complex issue on Windows. I am trying to write a UI control for a large application that shows a \'percentage of memory used\' number, in order to give the user an

I know that memory usage is a very complex issue on Windows.

I am trying to write a UI control for a large application that shows a 'percentage of memory used' number, in order to give the user an indication that it may be time to clear up some memory, or more likely restart the application.

One implementation used ullAvailVirtual from MEMORYSTATUSEX as a base, then used HeapWalk() to walk the process heap looking for additional free memory. The HeapWalk() step was needed because we noticed that after a while of running the memory allocated and freed by the heap was never returned and reported by the ullAvailVirtual number. After hours of intensive working, the ullAvailVirtual number no longer would accurately report the amount of memory available.

However, this method proved not ideal, due to occasional odd errors that HeapWalk() would return, even when the process heap was not corrupted. Further, since this is a UI control, the heap walking code was executing every 5-10 seconds. I tried contacting Microsoft about why HeapWalk() was failing, escalated a case via MSDN, but never got an answer other than "you probably shouldn't do that".

So, as a second implementation, I used PagefileUsage from PROCESS_MEMORY_COUNTERS as a base. Then I used VirtualQueryEx to walk the virtual address space adding up all regions that weren't MEM_FREE and returned a value for GetMappedFileNameA(). My thinking was that the PageFileUsage was essentially 'private bytes' so if I added to that value the total size of the DLLs my process was using, it would be a good approximation of the amount of memory my process was using.

This second method seems to (sorta) work, at least it doesn't cause crashes like the heap walker method. However, when both methods are enabled, the values are not the same. So one of the methods is wrong.

So, StackOverflow world...how would you implement this?

  • which method is more promising, or do you have a third, better method?
  • should I go back to the original method, and further debug the odd errors?
  • should I stay away from walking the heap every 5-10 seconds?

Keep in mind the whole point is to indicate to the user that it is getting 'dangerous', and they should either free up memory or restart the application. Perhaps a 'percentage used' isn't the best开发者_StackOverflow社区 solution to this problem? What is? Another idea I had was a color based system (red, yellow, green, which I could base on more factors than just a single number)


Yes, the Windows memory manager was optimized to fulfill requests for memory as quickly and efficiently possible, it was not optimized to easily measure how much space is used. The first downfall is that heap blocks that are released are rarely unmapped. They are simply marked as "free", to be used by the next allocation. That's why VirtualQueryEx() cannot work.

The problem with HeapWalk is that you have to lock the heap (HeapLock) so that it can walk it without the heap allocation changing. That lock can have very detrimental side-effects. Quoting:

Walking a heap may degrade performance, especially on symmetric multiprocessing (SMP) computers. The side effects may last until the process ends.

Even then, the number you get back is pretty meaningless. A program never runs out of free space, it runs out of a large enough contiguous chunk of memory to fulfill the request. No happy answers I'm afraid. Except one: a 64-bit operating system cost less than two hundred bucks.


The place to start is probably GetProcessMemoryInfo(). This fills in a structure for you that has, among other things, the current working set in bytes.


Have a look at the following article .NET and running processes

It uses WMI to check for the memory usage of processes, specifically using the

System.Diagnostics.Process

and another link on how to use WMI in C#: WMI Made Easy for C#

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号