Windows OSes and Memory Management— What happens when an application is minimized?

北战南征 提交于 2019-12-10 10:12:37

问题


I'm asking this question for posterity reasons and general interest. Years ago I noticed an odd behavior within Windows. I know this works in XP and at that time it also worked in Windows 2000 and even 98 and I'd assume this behavior still works for Windows 7.

To setup my question, try this experiment by following these steps.

  1. Open the Task Manager and navigate to the Processes tab.
  2. Open an application that you use that you know consumes a fair amount of memory (a web browser should work fine) and find the process in the Task Manager.
  3. Now use the application by opening windows, documents, web pages, etc. until the Memory Usage of your application grows significantly.
  4. Make note of the applications Memory Usage and minimize the application-- you should see a significant reduction in the amount of memory that the application is using.
  5. Finally, restore the application.

After restoring the Window, you should that that the Memory Usage of the application should spike a little but no where near as much as it was before you minimized the application. This is quite consistent with any GUI based application but it is certainly more profound with larger applications.

QUESTION

Internally within the OS, what process is taking place that forces this (assumed) garbage collection to take place? Furthermore, via COM or some .Net calls is there a way to force this process without minimizing an application? If so, please provide source code. The language doesn't matter.

If you are like me, you may keep a lot of applications open at the same time. Memory usage can creep up... up... up.. and before you know it, you need to close a few applications to free resources. It would be nice if I could at least integrate into the software that I have control a mechanism to force garbage collection from the OS similar to the behavior from the exercise above.

I know that in .Net there is a GC object and you can call the garbage clean-up as you desire. It does not, however, seem to be related with the nature of the memory clean-up that happens when an application is minimized.

I hope this is a head scratcher and that after years of curiosity that someone- can give me a good, technical explanation of what is going on.

Page Files and Going Further Down the "Rabbit Hole"

Interesting stuff has been posted as an answer to this question by Kragen & Alex K. Upon reading the Knowledge Base article they both posted, I see that there is also a way to prevent this behavior from happening. What are some examples of when you would want to prevent this paging freeing from taking place? I'm trying to read through the documentation to learn as much about this as I can. A lot of this information is new to me because I've never needed to implement any of this within .Net applications. Considering a use-case would help me grasp the reasoning of why and when I would want to avoid this behavior.


回答1:


The process you are seeing is not garbage collection - Windows is "trimming the working set" (i.e. paging as much memory as it can to disk) in order to free up memory for foreground applications.

See the following links for more information:

  • The working set of an application is trimmed when its top-level window is minimized (KB)
  • SetProcessWorkingSetSize Function

The KB article linked above describes both how you can prevent this from automatically happening (by intercepting the WM_SYSCOMMAND command), and how you can explicitly cause this to happen at any time (by calling SetProcessWorkingSetSize).




回答2:


Minimizing a window can cause windows to trim the working set of that process for the benefit of the remaining foreground applications (accomplished by calling SetProcessWorkingSetSize()).



来源:https://stackoverflow.com/questions/6612469/windows-oses-and-memory-management-what-happens-when-an-application-is-minimiz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!