TaskManager中WPF应用程序的内存消耗 [英] Memory consumption of WPF application in TaskManager

查看:93
本文介绍了TaskManager中WPF应用程序的内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在做一个WPF应用程序。当我运行应用程序并检查TaskManager中的内存消耗时,当我们执行某些操作时,它会逐渐增加。我搞砸了这是WPF控件的行为还是我做错了什么。



我举一个例子:

我有两个包含一些值和两个按钮的文本框。

当我单击其中一个按钮时,1st TextBox的值复制到另一个。然后点击另一个按钮,反之(从第二个到第一个)复制。

每次点击都会增加内存消耗。 (案例包括ListBox等,..

请给我一些帮助。



CS代码如下。



private void button1_Click(object sender,RoutedEventArgs e)

{

ListBoxItem item =(PinnedListBox.SelectedItem as ListBoxItem) ;

PinnedListBox.Items.Remove(item);

UnPinnedListBox.Items.Add(item);

}



private void button2_Click(object sender,RoutedEventArgs e)

{

ListBoxItem item1 =(UnPinnedListBox.SelectedItem as ListBoxItem);

UnPinnedListBox.Items.Remove(item1);

PinnedListBox.Items.Add(item1);

}



在此先感谢

Karuppasamy

Hi there,

I am doing a WPF application. When I am running the application and check for the Memory consumption in TaskManager, it is getting gradual increase when we do some operations. I am messed up whether this is the behavior of WPF controls or I did something wrong.

Let me say an example:
I am having two textboxes that contains some values and two buttons.
When I click one of the button, the value of 1st TextBox copied to another. And on click of another button, the reverse (copying from 2nd to 1st) happens.
On each every click the Memory consumption increases. (The case includes ListBox, etc.,..
Please give me some assistance.

The CS code as follows.

private void button1_Click(object sender, RoutedEventArgs e)
{
ListBoxItem item = (PinnedListBox.SelectedItem as ListBoxItem);
PinnedListBox.Items.Remove(item);
UnPinnedListBox.Items.Add(item);
}

private void button2_Click(object sender, RoutedEventArgs e)
{
ListBoxItem item1 = (UnPinnedListBox.SelectedItem as ListBoxItem);
UnPinnedListBox.Items.Remove(item1);
PinnedListBox.Items.Add(item1);
}

Thanks in advance
Karuppasamy

推荐答案

您好KaruppasamyP,



您无法判断.NET的内存消耗这样的应用。在进入细节之前,尝试了解GC(GarbageCollector)正在做什么以及如何做。 (在您的情况下,您很可能会看到仅在需要时释放内存的行为,也许它还会回收一些内存......)。这是每个.NET开发人员应该知道的事情。



理论上,.NET也可以实现内存泄漏 - 但您的描述听起来并不像真正的内存泄漏。



我曾经(当我没有经验的.NET时)从图表上的实时通信(硬件)进行过多的数据显示,机器上的内存压力时得到了紧张很低(很多RAM),所以内存增长到GB区域。然后GC开始了,内存被释放了...



所以我认为你不必担心内存泄漏 - 但也许是学习一些东西的好时机关于.NET的内部工作原理(并且内存管理并不像人们想象的那么简单......)



亲切的问候



Johannes
Hi KaruppasamyP,

You can not judge the memory consumption of an .NET application like that. Before going into details try to understand what the GC (GarbageCollector) is doing and how. (In your case most likely you see the behaviour that memory is freed only when needed, maybe it's also recycling some memory...). This is something every .NET developer should know anyway.

In theory memory leaks are also possible with .NET - but your description doesn't sound like a real memory leak.

I once (when I was unexperienced with .NET) got nervouse when doing excessive data presentation from a real time communication (Hardware) on a chart, memory pressure on machine was low (a lot of RAM), so memory was growing to the GB area. But then GC kicked in and memory was freed...

So I think you don't have to worry about a memory leak - but maybe good time to learn something about the inner workings of .NET (and memory management is not such an trivial task as someone may think...)

Kind regards

Johannes


这篇关于TaskManager中WPF应用程序的内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆