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

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

问题描述

什么是一些提示,以减少.NET应用程序的内存使用情况?考虑下面简单的C#程序。

 类节目
{
    静态无效的主要(字串[] args)
    {
        到Console.ReadLine();
    }
}
 

在编译的发布模式的 64 和Visual Studio之外运行,任务管理器报告如下:

 工作集:9364k
私人工作集:2500K
提交大小:17480k
 

这是好一点,如果它只是编译为 86

 工作集:5888k
私人工作集:1280k
提交大小:7012k
 

然后我尝试下面的程序,它不相同,但试图削减进程大小运行时初始化后:

 类节目
{
    静态无效的主要(字串[] args)
    {
        minimizeMemory();
        到Console.ReadLine();
    }

    私有静态无效minimizeMemory()
    {
        所以GC.Collect(GC.MaxGeneration);
        GC.WaitForPendingFinalizers();
        SetProcessWorkingSetSize(Process.GetCurrentProcess()处理,
            (UIntPtr)0xFFFFFFFF的,(UIntPtr)0xFFFFFFFF的);
    }

    [的DllImport(KERNEL32.DLL)
    [返回:的MarshalAs(UnmanagedType.Bool)
    私人静态外部布尔SetProcessWorkingSetSize(IntPtr的过程中,
        UIntPtr minimumWorkingSetSize,UIntPtr maximumWorkingSetSize);
}
 

在结果的 86 *的发布的*的Visual Studio之外的:

 工作集:2300K
私人工作集:964k
提交大小:8408k
 

哪个更好一点,但它似乎仍然过大这样一个简单的程序。有没有什么技巧,使一个C#程序有点精简?我正在写的设计,以在后台运行的大部分时间的程序。我已经在做任何用户界面东西,在一个单独的应用领域这意味着用户界面的东西可以安全地卸载,但占10 NBSP; MB,当它只是坐在后台似乎过多的

PS 至于为什么我会在意---(电源)的用户往往担心这些事情。即使它几乎没有对性能的影响,半精通技术的用户(我的目标受众)往往进入嘘声符合有关后台应用程序的内存使用情况。打MB即使;即使我发疯,当我看到的Adobe更新程序以11  MB的内存和感觉的平静触摸的Foobar2000的,这可能需要在6 NBSP安慰。我知道,在现代的操作系统,这东西真的没关系那么多技术,但是,这并不意味着它不会对认知的影响。

解决方案
  1. 您可能想看看堆栈溢出问题的 .NET EXE内存占用
  2. 在MSDN博客文章的 工作集!=实际的内存占用 的是关于玄机的工作集,进程内存和如何在总进行精确的计算在-RAM的消耗。

我不会说你应该忽略你的应用程序的内存占用 - 显然,体积更小,效率也往往是可取的。但是,你应该考虑你的实际需求是什么。

如果你正在写一个标准的Windows窗体和WPF客户端应用程序是注定要一个人的PC上运行,并且很可能是在用户操作,你可以逃脱更加无精打采的关于内存分配的主要应用程序。 (只要它全部被释放。)

不过,以解决一些人在这里谁说​​不担心:如果你正在写一个Windows窗体,应用程序,将运行在终端服务环境可能10,20或更多的用户使用一个共享的服务器上,那么,你绝对必须考虑内存使用情况。你会需要保持警惕。解决这一问题的最好办法是具有良好的数据结构设计,并按照有关何时以及你分配的最佳做法。

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

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

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

The results on x86 *Release* outside Visual Studio:

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. You might want to check out Stack Overflow question .NET EXE memory footprint.
  2. The MSDN blog post Working set != actual memory footprint is all about demystifying the working set, process memory and how to perform accurate calculations on your total in-RAM consumption.

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.

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.)

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天全站免登陆