简体   繁体   English

如何减少任务管理器中应用程序对 memory 的消耗

[英]How to reduce memory consumption by an application in Task manager

I have developed a winforms application using C# in VS2008.我在 VS2008 中使用 C# 开发了一个 winforms 应用程序。 When I run this application I observed in task manager that it shows 80 MB of memory is consumed.当我运行此应用程序时,我在任务管理器中观察到它显示 80 MB 的 memory 已消耗。 How can I reduce this?我怎样才能减少这种情况? Even very small applications also take 8 MB of memory...即使是非常小的应用程序也需要 8 MB 的 memory...

What can I do to reduce the memory footprint?我可以做些什么来减少 memory 占用空间?

Thank you very much in advance.非常感谢您提前。

The task manager memory number is not always as sharp as it would be, however this small trick can cheat that number... Not meant for production任务管理器 memory 编号并不总是像它应该的那样清晰,但是这个小技巧可以欺骗该编号......不适用于生产

public static void RefreshMemory() {
    try {
        Process curProc = Process.GetCurrentProcess();
        curProc.MaxWorkingSet = curProc.MaxWorkingSet;
    } catch {
        // Handle the exception
    }
}

It would be helpful also to trace the memory objects usage with a tool like JetBrains dotTrace or another similar.使用 JetBrains dotTrace 或其他类似工具跟踪 memory 对象的使用情况也会很有帮助。

Well, the .NET framework has a fairly significant overhead -- the simplest possible "hello world" console app has a 4 mb working set, as you have observed.嗯,.NET 框架的开销相当大——正如您所观察到的,最简单的“hello world”控制台应用程序有一个 4 mb 的工作集。 There are a number of things you can do to reduce the memory footprint (reduce embedded resources, be sure to build in Release configuration, etc.)您可以做很多事情来减少 memory 占用空间(减少嵌入式资源,确保在发布配置中构建等)

But at the end of the day, .NET is designed for developer efficiency over memory/resource efficiency, so if you have an app that has to run in a very small efficient memory space you should consider writing it in C++ or some other language where you manage your own resources.但归根结底,.NET 是专为开发人员效率而不是内存/资源效率而设计的,因此,如果您的应用程序必须在非常小的高效 memory 空间中运行,您应该考虑用 ZF6F87C9FDCF8B3C3F097F93F1EE8 或其他语言编写它管理自己的资源。

Your question clearly smells of premature optimization.您的问题显然有过早优化的味道。

You should tackle memory usage only in the following situations:您应该仅在以下情况下处理 memory 的使用:

  1. You're developing for a device which hasn't a lot of memory available (with the portable .net framework for example)您正在为没有很多可用 memory 的设备进行开发(例如,使用便携式 .net 框架)
  2. You're reaching the limit of .net in term of memory size (ie around 1.3gbp)就 memory 大小(即大约 1.3gbp)而言,您已达到 .net 的限制
  3. Your customers are complaining.你的客户在抱怨。

Don't get me wrong, this doesn't mean you must waste memory or that you shouldn't take memory in consideration while coding.不要误解我的意思,这并不意味着您必须浪费 memory 或者您在编码时不应该考虑 memory。 I just mean that in your case, memory usage is probably not a big deal.我只是说,在您的情况下,memory 的使用可能没什么大不了的。

In addition to all the previous good comments, please remember that the numbers you are seeing in Task Manager are virtual memory , not physical memory.除了之前所有的好评之外,请记住您在任务管理器中看到的数字是虚拟 memory ,而不是物理 memory。 The actual amount of physical RAM used by the program is not obvious from looking at Task Manager.通过查看任务管理器,程序使用的实际物理 RAM 量并不明显。 Also, Task Manager is showing you a series of snapshots - if you really had a problem, you'd want to look at it over time, with Perfmon or something, in addition to the excellent suggestion on profiling with JetBrains' dotTrace.此外,任务管理器会向您显示一系列快照 - 如果您真的有问题,您会想随着时间的推移使用 Perfmon 或其他东西来查看它,此外还有关于使用 JetBrains 的 dotTrace 进行分析的出色建议。

But don't do any optimization until your code works, and has high test coverage, and until you know what actually needs to be optimized.但是,在您的代码正常工作并且具有较高的测试覆盖率并且知道实际需要优化什么之前,不要进行任何优化。 Otherwise, you run the risk of optimizing problems that don't exist, and worse, of optimizing the wrong problem, while ignoring the real problem or possibly making it worse.否则,您将面临优化不存在的问题的风险,更糟糕的是,优化错误的问题,而忽略真正的问题或可能使问题变得更糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM