Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the memory usage of a WPF app

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.

like image 778
Morten Christiansen Avatar asked Dec 23 '08 14:12

Morten Christiansen


People also ask

How do I reduce memory usage in WPF?

Try this: Open the task manager, go to the processes tab, go to view, select collums and check the box that says VM size. Notice that when you minimize or restore a program or application the memory changes from what's in Memory Usage to VM size.

Can Android run WPF?

. NET 5 is s unified platform to write code once and run anywhere.


3 Answers

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

like image 192
Rob Avatar answered Sep 20 '22 13:09

Rob


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.

like image 39
Roger Lipscombe Avatar answered Sep 20 '22 13:09

Roger Lipscombe


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 /?"

like image 41
Steve Hanov Avatar answered Sep 19 '22 13:09

Steve Hanov