减少 .NET 应用程序的内存使用量? [英] Reducing memory usage of .NET applications?

查看:23
本文介绍了减少 .NET 应用程序的内存使用量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些技巧可以减少 .NET 应用程序的内存使用量?考虑以下简单的 C# 程序.

What are some tips to reduce the memory usage of .NET applications? Consider the following simple C# program.

class Program
{
    static void Main(string[] args)
    {
        Console.ReadLine();
    }
}

x64发布模式下编译并在 Visual Studio 外运行,任务管理器报告以下内容:

Compiled in release mode for x64 and running outside Visual Studio, the task manager reports the following:

Working Set:          9364k
Private Working Set:  2500k
Commit Size:         17480k

如果只为 x86 编译会好一点:

It's a little better if it's compiled just for x86:

Working Set:          5888k
Private Working Set:  1280k
Commit Size:          7012k

然后我尝试了以下程序,该程序执行相同但尝试在运行时初始化后调整进程大小:

I then tried the following program, which does the same but tries to trim process size after runtime initialization:

class Program
{
    static void Main(string[] args)
    {
        minimizeMemory();
        Console.ReadLine();
    }

    private static void minimizeMemory()
    {
        GC.Collect(GC.MaxGeneration);
        GC.WaitForPendingFinalizers();
        SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
            (UIntPtr) 0xFFFFFFFF, (UIntPtr)0xFFFFFFFF);
    }

    [DllImport("kernel32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetProcessWorkingSetSize(IntPtr process,
        UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize);
}

x86 Release 在 Visual Studio 之外的结果:

The results on x86 Release outside Visual Studio:

Working Set:          2300k
Private Working Set:   964k
Commit Size:          8408k

哪个好一点,但是对于这么简单的程序来说还是显得过分了.有什么技巧可以让 C# 过程更精简一点吗?我正在编写一个大部分时间都在后台运行的程序.我已经在一个单独的应用域中做任何用户界面的东西,这意味着用户界面的东西可以可以安全卸载,但是当它只是坐在后台时占用 10 MB 似乎过多.

Which is a little better, but it still seems excessive for such a simple program. Are there any tricks to make a C# process a bit leaner? I'm writing a program that's designed to run in the background most of the time. I'm already doing any user interface stuff in a separate Application Domain which means the user interface stuff can be safely unloaded, but taking up 10 MB when it's just sitting in the background seems excessive.

P.S. 至于我为什么会关心 ---(高级)用户往往会担心这些事情.即使它对性能几乎没有影响,半精通技术的用户(我的目标受众)往往会对后台应用程序内存使用情况大发雷霆.甚至当我看到 Adob​​e Updater 占用 11 MB 内存时我也感到害怕,而 Foobar2000 的平静触感让我感到安慰,即使在玩游戏时它也可以占用不到 6 MB.我知道在现代操作系统中,这些东西在技术上真的没有那么重要,但这并不意味着它不会影响感知.

P.S. As to why I would care --- (Power)users tend to worry about these things. Even if it has nearly no effect on performance, semi-tech-savvy users (my target audience) tend to go into hissy fits about background application memory usage. Even I freak when I see Adobe Updater taking 11 MB of memory and feel soothed by the calming touch of Foobar2000, which can take under 6 MB even when playing. I know in modern operating systems, this stuff really doesn't matter that much technically, but that doesn't mean it doesn't have an affect on perception.

推荐答案

  1. 您可能想查看堆栈溢出问题.NET EXE 内存占用.
  2. MSDN 博客文章工作集 != 实际内存占用 是关于揭开工作集、进程内存以及如何对您的数据执行准确计算的神秘面纱内存消耗总量.

我不会说您应该忽略应用程序的内存占用——显然,更小、更高效的做法确实是可取的.但是,您应该考虑您的实际需求.

I will not say that you should ignore the memory footprint of your application -- obviously, smaller and more efficient does tend to be desirable. However, you should consider what your actual needs are.

如果您正在编写一个标准的 Windows 窗体和 WPF 客户端应用程序,它注定要在个人 PC 上运行,并且可能是用户操作的主要应用程序,那么您可以避免对内存分配更加松懈.(只要这一切都被解除分配.)

If you are writing a standard Windows Forms and WPF client applications which is destined to run on an individual's PC, and is likely to be the primary application in which the user operates, you can get away with being more lackadaisical about memory allocation. (So long as it all gets deallocated.)

但是,这里有一些人说不用担心:如果您正在编写将在终端服务环境中运行的 Windows 窗体应用程序,则在可能由 10、20 或更多用户使用的共享服务器上,那么是的,您绝对必须考虑内存使用情况.你需要保持警惕.解决此问题的最佳方法是采用良好的数据结构设计,并遵循有关分配时间和分配内容的最佳做法.

However, to address some folks here who say not to worry about it: If you're writing a Windows Forms application which will be running in a terminal services environment, on a shared server possibly utilized by 10, 20 or more users, then yes, you absolutely must consider memory usage. And you will need to be vigilant. The best way to address this is with good data structure design and by following best practices regarding when and what you allocate.

这篇关于减少 .NET 应用程序的内存使用量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆