.NET EXE内存占用内存、NET、EXE

2023-09-02 10:25:53 作者:莂懼丗俗

即使是一个简单的记事本在C#应用程序使用的RAM兆字节在任务管理器中看到。在最小化应用程序在任务管理器中的内存容量下降显着,是备份应用程序时最大化。

Even a simple Notepad application in C# consumes megabytes of RAM as seen in the task manager. On minimizing the application the memory size in the task manager goes down considerably and is back up when the application is maximized.

我读的地方,在.NET过程中保留了大量的内存运行时的分配提前。这就是为什么.NET应用程序有较大的内存占用下手。但是这种内存可以使用Win32 API调用被释放。折衷的是,运行时的分配变得很慢 - 这是真的。

I read somewhere that the .NET process reserves a lot of memory for runtime allocation in advance. That's why .NET applications have a larger memory footprint to start with. But this memory can be released using Win32 API calls. A trade-off is that runtime allocation becomes slow - is that true?

推荐答案

任务管理器不应该被用来衡量一个.NET应用程序的内存占用。

TaskManager should not be used to measure the memory footprint of a .NET application.

在.NET应用程序启动时,它会要求操作系统的内存块,它再细分成为托管堆,栈和大型对象堆。它是内存的任务管理正在报告,其可以或可以不完全被.NET用这个总块。一旦.NET应用程序给出的存储器块,也不会释放它,直到询问由OS,这只会发生与OS确定需要更多的内存资源。

When a .NET application starts, it asks the OS for a chunk of memory which it then segments to become the managed heap, stack, and large object heap. It is this total chunk of memory that TaskManager is reporting, which may or may not be completely used by .NET. Once a .NET application is given a chunk of memory, it will not release it until asked by the OS, which will only happen with the OS determines a need for more memory resources.

如果你想测量内存分配,你需要看看各种性能监视器(PerfMon)计数器。

If you want to measure memory allocations, you need to look at the various performance monitor (PerfMon) counters.

您可以使用互操作code来调用Win32 API来修剪你的工作集大小,但是从操作系统的下一次你的应用程序请求的内存工作集就会涨回去会有,而操作系统的性能命中分配和手了额外的内存和.NET运行时配置了。

You can use interop code to call Win32 APIs to trim your working set size, but the next time your application requests memory from the OS the working set will go back up and there will be a performance hit while the OS allocates and hands out the additional memory and the .NET runtime "configures" it.