本文介绍了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 situationI have the following routine running repeatedly (curControl is a UserControlwith say 1000 textboxes and a big array of strings): Public Sub AddControl(ByVal ctlName As String)If Not IsNothing(curControl) ThenMe.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) ThencurControl.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 isjust 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 iscleaned up properly - but in the second scenario (where Dispose is actuallybeing closed), it is not. The memory keeps climbing up and up. Thanks推荐答案 UserControl UserControl 相同。 是 实际上 这篇关于Windows窗体中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 14:01