Windows 操作系统和内存管理——最小化应用程序时会发生什么?

发布于 2024-11-18 16:53:32 字数 1205 浏览 3 评论 0原文

我问这个问题是为了子孙后代的利益和普遍利益。几年前,我注意到 Windows 中有一个奇怪的行为。我知道这在 XP 中有效,当时它在 Windows 2000 甚至 98 中也有效,我认为这种行为在 Windows 7 中仍然有效

。要设置我的问题,请按照以下步骤尝试此实验。

  1. 打开任务管理器并导航到“进程”选项卡。
  2. 打开您使用的、已知会消耗大量内存的应用程序(Web 浏览器应该可以正常工作),然后在任务管理器中找到该进程。
  3. 现在通过打开窗口、文档、网页等来使用应用程序,直到应用程序的内存使用量显着增长。
  4. 记下应用程序的内存使用情况并最小化应用程序 - 您应该看到应用程序使用的内存量显着减少。
  5. 最后,恢复应用程序。

恢复窗口后,您应该看到应用程序的内存使用情况会略有增加,但不会像最小化应用程序之前那样多。这与任何基于 GUI 的应用程序都非常一致,但对于较大的应用程序来说肯定更深刻。

问题

在操作系统内部,正在发生什么进程来强制执行此(假设的)垃圾收集?此外,通过 COM 或一些 .Net 调用,有没有办法在不最小化应用程序的情况下强制执行此过程?如果有,请提供源代码。语言并不重要。

如果您像我一样,您可能会同时打开很多应用程序。内存使用量可能会逐渐增加...增加...增加..,在您意识到之前,您需要关闭一些应用程序以释放资源。如果我至少可以将一种机制集成到我所控制的软件中,以强制从操作系统进行垃圾收集,类似于上面练习中的行为,那就太好了。

我知道.Net中有一个GC对象,你可以根据需要调用垃圾清理。然而,它似乎与最小化应用程序时发生的内存清理的性质无关。

我希望这是一个令人头疼的问题,并且在多年的好奇心之后,有人可以给我一个关于正在发生的事情的良好的、技术性的解释。

页面文件和进一步深入“兔子洞”

Kragen & 已发布有趣的内容作为此问题的答案。 Alex K. 在阅读他们发布的知识库文章后,我发现有也是防止这种行为发生的一种方法。您希望阻止这种分页释放发生的一些例子有哪些?我正在尝试阅读文档以尽可能多地了解这一点。其中很多信息对我来说都是新的,因为我从来不需要在 .Net 应用程序中实现任何这些信息。考虑一个用例将帮助我理解为什么以及何时想要避免这种行为。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2024-11-25 16:53:32

您看到的进程不是垃圾收集 - Windows 正在“修剪工作集”(即,将尽可能多的内存分页到磁盘),以便为前台应用程序释放内存。

有关详细信息,请参阅以下链接:

上面链接的知识库文章描述了如何防止这种情况自动发生(通过拦截 WM_SYSCOMMAND 命令),以及如何显式地导致这种情况随时发生(通过调用 SetProcessWorkingSetSize)。

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 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).

Spring初心 2024-11-25 16:53:32

最小化窗口可能会导致窗口修剪该进程的工作集,以利于剩余的前台应用程序(通过调用 SetProcessWorkingSetSize())。

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()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文