问题描述
我正在使用WPF开发一个用C#编写的小书签管理应用程序.它仅位于系统托盘中,并且有99%的时间处于空闲状态.最近,我查看了任务管理器,发现它使用了大约25兆的内存(第一次激活前大约需要12兆的内存),我认为这对于大多数情况下不执行任何操作的应用程序来说有点过多.这让我想知道是否有任何方法可以通过例如禁用可选的WPF功能来减少内存使用.
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.
我发现了一个可能导致某些结果的事实,尽管我不知道有什么方法可以利用它..NET中的每个线程大约需要一个兆,而我的应用程序使用了大约6/12个线程(在第一次激活之前和之后).这占了我相当大的内存使用量的一半.我没有直接产生任何新线程,但是我不知道WPF以及.NET的其他部分如何将线程用于不同的任务,因此我很难做任何事情.将事件用于与GUI不直接相关的内容,例如,这会产生新线程吗?
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?
所以我想我的问题是双重的,如何才能总体上减少.NET/WPF应用程序的内存使用,又如何最大程度地减少生成的线程数?请注意,我不是在考虑一些小细节,例如此答案中提到的那些细节.,而是设计如何在整个应用程序中降低内存使用量.
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.
推荐答案
如果它是系统任务栏应用程序,则可以让该部分程序在WinForms(甚至C ++)中实现,并且仅当用户使用时才生成WPF应用程序.双击您的图标.这样,您才需要在实际使用内存时付费.
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.
这篇关于如何减少WPF应用程序的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!