.NET EXE 内存占用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/223283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET EXE memory footprint
提问by dotnetcoder
Even a simple Notepadapplication 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.
正如在任务管理器中看到的那样,即使是一个简单的C#记事本应用程序也会消耗数兆字节的 RAM。在最小化应用程序时,任务管理器中的内存大小会大大减少,并在应用程序最大化时备份。
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 进程预先为运行时分配保留了大量内存。这就是 .NET 应用程序开始时需要更大内存占用的原因。但是可以使用 Win32 API 调用释放此内存。权衡是运行时分配变慢 - 这是真的吗?
回答by Ed Altorfer
The reason for the large memory footprint is that the JIT compiler and Windows Formsengine are being loaded with your process. To reduce this, you can do the following:
占用大量内存的原因是 JIT 编译器和Windows 窗体引擎正在与您的进程一起加载。为了减少这种情况,您可以执行以下操作:
[DllImport("psapi.dll")]
static extern int EmptyWorkingSet(IntPtr hwProc);
static void MinimizeFootprint()
{
EmptyWorkingSet(Process.GetCurrentProcess().Handle);
}
This should remove as much as possible from your memory footprint. There may be a way that you can also reduce the amount of memory that is set aside for runtime memory allocation.
这应该从您的内存占用中尽可能多地删除。可能还有一种方法可以减少为运行时内存分配预留的内存量。
回答by Scott Dorman
TaskManager should not be used to measure the memory footprint of a .NET application.
不应使用 TaskManager 来测量 .NET 应用程序的内存占用。
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.
当 .NET 应用程序启动时,它会向操作系统请求一块内存,然后将其分段以成为托管堆、堆栈和大对象堆。TaskManager 报告的正是这个总内存块,它可能会或可能不会被 .NET 完全使用。一旦 .NET 应用程序获得了一块内存,它就不会释放它,直到操作系统提出要求,这只会发生在操作系统确定需要更多内存资源的情况下。
If you want to measure memory allocations, you need to look at the various performance monitor (PerfMon) counters.
如果要测量内存分配,则需要查看各种性能监视器 (PerfMon) 计数器。
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.
您可以使用互操作代码调用 Win32 API 来调整您的工作集大小,但是下次您的应用程序从操作系统请求内存时,工作集将恢复运行,并且在操作系统分配和分发额外的内存时会出现性能下降内存和 .NET 运行时“配置”它。
回答by Joshua Hudson
The task manager does not show real life usage of memory for a .NET app. To see that you almost have to put a performance counter on the app or use a profiler.
任务管理器不会显示 .NET 应用程序的实际内存使用情况。要查看您几乎必须在应用程序上放置性能计数器或使用分析器。
What you see in the Task Manager is the working memory of an app which includes a bunch of overhead for the framework itself which must also load when your app loads.
您在任务管理器中看到的是应用程序的工作内存,其中包括框架本身的大量开销,这些开销也必须在您的应用程序加载时加载。

