问题描述
我用 C# 创建了一个程序.该程序使用了我大约 60-70MB 的内存.但是当我最小化那个程序时,它需要更少的内存,即只有 10 MB.
I had created a program in C#. That program used about 60-70 MB of my memory. But when I minimized that program, it required less memory, that is, just 10 MB.
当我最大化或回到那个程序时,它使用了 20 MB...
When I maximized or came back to that program it used 20 MB...
为什么会这样?
当您最小化程序时是否会调用垃圾收集器?
Is it true that garbage collector gets called when you minimize the progam?
推荐答案
本文一一讲解:记忆之谜
这是来自上述网站的摘录:
this is an extract from the above site:
.Net Urban Legend
可以减少 .Net Windows 窗体的工作集大小通过最小化然后最大化应用程序来应用程序加载后立即.Windows 操作系统修剪工作集当它们被最小化时的应用程序.短暂使用的内存在加载前面提到的所有这些程序集时,最小化和最大化应用程序的过程.你可以通过创建和运行仅包含 Form1 且未添加代码的 Windows 窗体应用程序.
It is possible to reduce the working set size of a .Net Windows Forms application by minimizing and then maximizing the application immediately after it loads. Windows OS trims the working set of applications when they are minimized. The memory that was briefly used while loading all those assemblies mentioned earlier is trimmed by the process of minimizing and maximizing the application. You can demonstrate this behavior to yourself by creating and running a Windows Forms application with just Form1 and no added code.
创建并运行简单的应用程序.
Create and run the simple application.
打开 Windows 任务管理器,然后打开其进程选项卡.您将看到任务管理器显示您的应用程序的内存使用情况大约为.12.5 MB.
Open the Windows Task Manager and then its Processes Tab. You will see the Task Manager shows your application’s memory usage at approx. 12.5 MB.
现在最小化您的应用程序,然后再最大化它.再次检查任务管理器.您将看到任务管理器现在显示您的应用程序的内存使用量约为1.5 MB.使用的内存在您的应用程序启动时加载程序集被回收最小化应用程序时的内存管理.
Now minimize your application and then maximize it. Check the Task Manager again. You will see the Task Manager now shows your application’s memory usage at approx. 1.5 MB. The memory that was used to load assemblies when your application was launched was reclaimed by memory management when you minimized the application.
您是否通过最小化和优化内存管理或应用程序性能?最大化您的应用程序?不,你可以找到一些 .Net Windows添加代码以最小化然后最大化他们的程序的程序员认为他们正在优化内存.正如这些程序员所拥有的与其他人分享了这种技术,一种 .Net Urban Legend 已经出生——一种基于虚构而非事实的编程实践.这实践是不必要的,因为当操作系统需要剩余未使用的内存时加载程序集后,它将回收它自动地.事实上,减小应用程序的大小内存充足时的工作集可能会降低性能.
Did you improve memory management or application performance by minimizing and maximizing your application? No. You can find some .Net Windows programmers who add code to minimize and then maximize their programs thinking that they are optimizing memory. As these programmers have shared this technique with others a sort of .Net Urban Legend has been born – a programming practice based on fiction not fact. This practice is unnecessary because when OS needs the unused memory left behind when the assemblies were loaded it will reclaim it automatically. In fact, decreasing the size of your application’s working set when memory is plentiful may decrease performance.
这篇关于在 .NET 中,当您最小化程序时会调用垃圾收集器,这是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!