Windows窗体中的内存 [英] Memory in windows forms

查看:59
本文介绍了Windows窗体中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请考虑以下情况

我有以下例程反复运行(curControl是一个UserControl

,比如1000个文本框和一大串字符串):


Public Sub AddControl(ByVal ctlName As String)

If Not IsNothing(curControl)然后

Me.Controls.Remove(curControl)

结束如果


Dim assemb As [Assembly] = [Assembly] .Load(" MyControlAssembly")

curControl = CType(assemb.CreateInstance(ctlName),UserControl)

curControl.Location =新点(150,20)

Me.Controls .Add(curControl)

结束子


现在,一遍又一遍地运行这个Sub,内存大致保持不变。

也就是说,它上升了一点点 - 可能是4-20K,但没什么大的。


现在,考虑这个版本:


Public Sub AddControl(ByVal ctlName As String)

If Not IsNothing(curControl)那么

curCo ntrol.Dispose()

结束如果


Dim assemb As [Assembly] = [Assembly] .Load(" MyControlAssembly")

curControl = CType(assemb.CreateInstance(ctlName),UserControl)

curControl.Location =新点(150,20)

结束子


现在,在这个版本中,控件没有添加到表单中 - 它刚刚创建了




例如,每次运行此代码时内存大约增加0.5 MB。


现在,问题是,为什么在第一种情况下,控件是

正确清理 - 但在第二种情况下(Dispose实际上是关闭
),事实并非如此。记忆不断攀升。


谢谢

Hi,
Consider the following situation
I have the following routine running repeatedly (curControl is a UserControl
with say 1000 textboxes and a big array of strings):

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
Me.Controls.Remove(curControl)
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
Me.Controls.Add(curControl)
End Sub

Now, running this Sub over and over, the memory stays more or less the same.
That is to say, it goes up a litte bit - maybe 4-20K, but nothing huge.

Now, consider this version:

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
curControl.Dispose()
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
End Sub

Now, in this version, the control is not being added to the form - it is
just created.

In this case, the memory goes up by about .5 MB every time this code runs.

Now, the questions is, why is it that in the first scenario, the control is
cleaned up properly - but in the second scenario (where Dispose is actually
being closed), it is not. The memory keeps climbing up and up.

Thanks

推荐答案

在第二个例子中,你''重新调用dispose,但你没有删除

控件,因此对它的引用仍然存在。你应该在两种情况下调用

Me.Controls.Remove(curControl)然后调用curControl.Dispose()。这是

不是这两种情况。


Pete


" Marina" <所以***** @ nospam.com>在消息中写道

新闻:uZ ************** @ TK2MSFTNGP09.phx.gbl ...
In the second example, you''re calling dispose, but you''re not removing the
control, so a reference to it remains. You should call
Me.Controls.Remove(curControl) then curControl.Dispose() in both case. It''s
not an either/or situation.

Pete

"Marina" <so*****@nospam.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
考虑以下情况
我有以下例程重复运行(curControl是
UserControl,比如1000个文本框和一大串字符串):

Public Sub AddControl(ByVal ctlName As String)
如果不是IsNothing(curControl)那么
Me.Controls.Remove(curControl)
结束如果

Dim assemb As [Assembly] = [Assembly ] .Load(" MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName),UserControl)
curControl.Location =新点(150,20)
Me.Controls.Add (curControl)
End Sub

现在,一遍又一遍地运行这个Sub,内存保持或多或少
相同。也就是说,它上升了一点点 - 可能是4-20K,但没什么大的。

现在,考虑这个版本:

Public Sub AddControl(ByVal ctlName As字符串)
如果不是IsNothing(curControl)那么
curControl.Dispose()
结束如果

Dim assemb As [Assembly] = [Assembly] .Load(" ; MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName),UserControl)
curControl.Location =新点(150,20)
结束子

现在,在这个版本中,控件没有添加到表单中 - 它刚刚创建。

在这种情况下,每次此代码时内存大约增加0.5 MB现在,问题是,为什么在第一种情况下,控制
被正确清理 - 但在第二种情况下(Dispose实际上是
)被关闭),事实并非如此。记忆不断攀升。

谢谢
Hi,
Consider the following situation
I have the following routine running repeatedly (curControl is a UserControl with say 1000 textboxes and a big array of strings):

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
Me.Controls.Remove(curControl)
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
Me.Controls.Add(curControl)
End Sub

Now, running this Sub over and over, the memory stays more or less the same. That is to say, it goes up a litte bit - maybe 4-20K, but nothing huge.

Now, consider this version:

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
curControl.Dispose()
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
End Sub

Now, in this version, the control is not being added to the form - it is
just created.

In this case, the memory goes up by about .5 MB every time this code runs.

Now, the questions is, why is it that in the first scenario, the control is cleaned up properly - but in the second scenario (where Dispose is actually being closed), it is not. The memory keeps climbing up and up.

Thanks



我为印刷错误道歉。添加控件的那一行实际上是在我的测试#2的实验中注释掉的。


< pd ****** @ hotmail。 COM>在消息中写道

新闻:15 ****************************** @ news.meganet新闻。 com ...
I apologize for the misprint. That line that adds the control is actually
commented out in my experiment for test #2.

<pd******@hotmail.com> wrote in message
news:15******************************@news.meganet news.com...
在第二个例子中,你正在调用dispose,但是你没有删除
控件,因此对它的引用仍然存在。你应该在两种情况下调用
Me.Controls.Remove(curControl)然后调用curControl.Dispose()。
这不是一种或两种情况。

Pete

Marina <所以***** @ nospam.com>在消息中写道
新闻:uZ ************** @ TK2MSFTNGP09.phx.gbl ...
In the second example, you''re calling dispose, but you''re not removing the
control, so a reference to it remains. You should call
Me.Controls.Remove(curControl) then curControl.Dispose() in both case. It''s not an either/or situation.

Pete

"Marina" <so*****@nospam.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...

考虑以下内容情况
我有以下例程反复运行(curControl是一个
Hi,
Consider the following situation
I have the following routine running repeatedly (curControl is a


UserControl


UserControl

,比如1000个文本框和一大串字符串):

Public Sub AddControl(ByVal ctlName As String)
如果不是IsNothing(curControl)那么
Me.Controls.Remove(curControl)
结束如果

昏暗assemb As [Assembly] = [Assembly] .Load(" MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName),UserControl)
curControl.Location = New Point(150,20)< br.> Me.Controls.Add(curControl)
End Sub

现在,一遍又一遍地运行这个Sub,内存保持或多或少的
with say 1000 textboxes and a big array of strings):

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
Me.Controls.Remove(curControl)
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
Me.Controls.Add(curControl)
End Sub

Now, running this Sub over and over, the memory stays more or less the


相同。

也就是说,它上升了一点点 - 可能是4-20K,但是没什么g huge。

现在,请考虑这个版本:

Public Sub AddControl(ByVal ctlName As String)
如果不是IsNothing(curControl)那么
curControl .Dispose()
结束如果

Dim assemb As [Assembly] = [Assembly] .Load(" MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName) ,UserControl)
curControl.Location =新点(150,20)
End Sub

现在,在这个版本中,控件没有被添加到表单中 - 它是
刚刚创建。

在这种情况下,每次代码
运行时内存大约增加0.5 MB。
现在,问题是,为什么是在第一种情况下,控件
That is to say, it goes up a litte bit - maybe 4-20K, but nothing huge.

Now, consider this version:

Public Sub AddControl(ByVal ctlName As String)
If Not IsNothing(curControl) Then
curControl.Dispose()
End If

Dim assemb As [Assembly] = [Assembly].Load("MyControlAssembly")
curControl = CType(assemb.CreateInstance(ctlName), UserControl)
curControl.Location = New Point(150, 20)
End Sub

Now, in this version, the control is not being added to the form - it is
just created.

In this case, the memory goes up by about .5 MB every time this code runs.
Now, the questions is, why is it that in the first scenario, the control


正确清理 - 但在第二种情况下(Dispose是
cleaned up properly - but in the second scenario (where Dispose is


实际上

正在关闭),事实并非如此。记忆不断攀升。

谢谢
being closed), it is not. The memory keeps climbing up and up.

Thanks




Hi Marina ,


我没有得到进一步的答案,我的想法是,你的用户控制中的处理可能不是很好。然而,这只是一个好处,因此这样的回答是很晚的。


在你的例子1中你设置了对控件的引用并删除了

参考

在你的例子2中你没有设置对控件的引用所以什么都没有

删除


所以即使没有处置,示例2应该与示例1相同。

这意味着在我看来,在处置中设置了引用。


但是只是猜测。


Cor
Hi Marina,

You got no further answers I see, my thought was that the dispose is maybe
not well done in your usercontrol. However just a gues and therefore such a
late answer.

In your example 1 you set a reference to the control and remove that
reference
In your example 2 you set no reference to the control so there is nothing to
remove

So example 2 should work the same as example 1 even without the dispose.
That would mean in my opinion that in the dispose a reference is set.

However just guessing.

Cor


这篇关于Windows窗体中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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