减少内存使用的一些技巧是什么.NET应用程序?考虑下面的简单C语言程序.

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

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

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

如果只为x86编译,会更好一些:

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

然后,我try 了以下程序,该程序执行相同的操作,但会在运行时初始化后try 调整进程大小:

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);
}

Visual Studio外部x86 Release上的结果:

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

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. 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. 您可能想查看堆栈溢出问题100.
  2. MSDN博客文章100都是关于揭开工作集、进程内存的神秘面纱,以及如何准确计算内存总消耗.

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

如果你正在编写一个标准的Windows窗体和WPF客户端应用程序,它注定要在个人电脑上运行,并且很可能是用户操作的主要应用程序,那么你可以不必对内存分配更懒散.(只要一切都被解除分配.)

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

.net相关问答推荐

EF核心类似功能T-SQL UPDATE FROM

.NET Blazor-使用子组件中的处理程序方法进行双向数据绑定

为什么DotNet新的webapi;命令会为我生成不同的文件夹

WinForm Task.Wait :为什么它会阻塞 UI?

Visual Studio 2022 中的目标操作系统和目标运行时有什么区别?

将字符串与容差进行比较

在 C# 中生成随机小数

如何创建 LINQ to SQL 事务?

如何从控制台应用程序中的 Task.WaitAll() 获取返回值?

如何从标头中检索基本身份验证凭据?

无法加载文件或程序集WebGrease,版本=1.5.1.25624,Culture=neutral,PublicKeyToken=31bf3856ad364e35或其依赖项之一

使用多个 MemoryCache 实例

为什么 C# 多维数组不实现 IEnumerable

HashSet 是否保留插入顺序?

非通用 TaskCompletionSource 或替代

DataGridView 在我的两个屏幕之一上的可怕重绘性能

何时何地使用 GetType() 或 typeof()?

MVC4 HTTP 错误 403.14 - 禁止

实体框架太慢了.我有哪些 Select ?

如何使用 AutoMapper .ForMember?