How to reduce the memory usage of a WPF app

12,429

Solution 1

Unfortunately, in my experience, ~25MB is the lowest I've seen for small WPF apps I've made, at least on Windows XP. I think even the empty template WPF apps take ~20MB. What OS are you running on?

Windows Vista is a better story, and you can probably expect to see ~13-15MB for an empty template WPF app.

For your app to use 6-12 threads and only use ~25MB, I'd say you're doing pretty well. :-)

Solution 2

If it's a system tray app, you could have that part of the program implemented in WinForms (or even C++), and only spawn the WPF application when the user double-clicks your icon. That way, you only pay for the memory when you're actually using it.

Solution 3

Not sure if this helps, but in MS Visual C++, the default stack size is 1 MB, and can be set to whatever you want using a compiler option. Obviously, C# apps inherited this default size (So each thread takes at minimum 1MB). But there doesn't seem to be anyway to set it when I do "csc /?"

Share:
12,429

Related videos on Youtube

Morten Christiansen
Author by

Morten Christiansen

I'm a software developer at UVdata in Denmark. I work on all sorts of interesting .NET technologies at work and in my spare time with a focus on the web. I have a passion for the craft/art of software development and love to explore the intricacies of good software design and code practices.

Updated on April 17, 2022

Comments

  • Morten Christiansen
    Morten Christiansen about 2 years

    I am working on a little bookmark managing app written in C#, using WPF. It just sits in the system tray and is idle 99% of the time. Recently I looked in the task manager and discovered that it uses around 25 megs of memory (and around 12 megs before it is activated for the first time) which I thought was a bit much for an app that does nothing most of the time. That made me wonder if there are any ways to reduce the memory usage by, e.g., disabling WPF features that are optional.

    I have discovered one fact which might lead to something, though I don't know any way to leverage it. Threads in .NET take around one meg each, and it turns out my app uses around 6/12 threads (before and after being activate for the first time). This accounts for half my memory usage which is rather significant. I dont spawn any new threads directly, but I have no idea of how WPF, as well as other parts of .NET, uses threads for different tasks so I find it hard to do anything about it. Using events for stuff that is not directly related to the GUI, does this for example spawn new threads?

    So I guess my question is twofold, how can you reduce the memory usage .NET/WPF applications in general and how can you minimize the number of threads being spawned? Note that I'm not so much thinking about small details such as those brought up in this answer, but rather how to design for low memory usage throughout your entire application.

  • Cristopher Van Paul
    Cristopher Van Paul over 15 years
    You will worry about it if you are asked to optimize it on terminal services, server admin don't want to see 30 idle processes waste around 1GB ram.
  • Morten Christiansen
    Morten Christiansen over 15 years
    You are, of course, right that I should not ignore the memory usage, and that is the reason for this post. But the particular app I'm building is just a little hobby project so it's not too bad. It does feel wrong, though, when such a small app uses that much memory.
  • Rob
    Rob over 15 years
    Yeah, I think you'll be okay - but if you test it on Windows XP, prepare to be shocked - I'm betting it will take a minimum of 60MB.