Note that there are some explanatory texts on larger screens.

plurals
  1. POReducing memory usage of .NET applications?
    primarykey
    data
    text
    <p>What are some tips to reduce the memory usage of .NET applications? Consider the following simple C# program.</p> <pre><code>class Program { static void Main(string[] args) { Console.ReadLine(); } } </code></pre> <p>Compiled in <strong>release</strong> mode for <strong>x64</strong> and running outside Visual Studio, the task manager reports the following:</p> <pre><code>Working Set: 9364k Private Working Set: 2500k Commit Size: 17480k </code></pre> <p>It's a little better if it's compiled just for <strong>x86</strong>:</p> <pre><code>Working Set: 5888k Private Working Set: 1280k Commit Size: 7012k </code></pre> <p>I then tried the following program, which does the same but tries to trim process size after runtime initialization:</p> <pre><code>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); } </code></pre> <p>The results on <strong>x86</strong> <strong>Release</strong> outside Visual Studio:</p> <pre><code>Working Set: 2300k Private Working Set: 964k Commit Size: 8408k </code></pre> <p>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 <a href="http://en.wikipedia.org/wiki/Application_Domain" rel="noreferrer">Application Domain</a> which means the user interface stuff can be safely unloaded, but taking up 10&nbsp;MB when it's just sitting in the background seems excessive.</p> <p><strong>P.S.</strong> 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&nbsp;MB of memory and feel soothed by the calming touch of Foobar2000, which can take under 6&nbsp;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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload