VB.NET 2010,框架 3.5
在完成自定义对象类型列表后,试图弄清楚如何取回内存。我知道强制列表超出范围会重新分配它正在使用的内存,但我需要一种务实的方法。有谁知道如何做到这一点?
Public Class List_Of_T_Test
Private MyTestClass As New List(Of TestClass)
Private Sub List_Of_T_Test_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Const LIST_COUNT As Integer = 5000000
Dim Count As Integer = 0
MsgBox("Note 'PF usage' on Windows Task Manager 'before the 5m list' is created")
Do Until Count > LIST_COUNT ' 5 million iterations
MyTestClass.Add(New TestClass With {.Value1 = CStr(Count), .Value2 = CStr(Count + 1)})
Count = Count + 1
Loop
MsgBox("Note 'PF usage' on Windows Task Manager 'after the 5m list' was created")
DestroyMyTestClassList()
MsgBox("When flow arrives here, I need MyTestClass to be gone, all memory to have been reallocated, or close to where it was before the list was created")
End Sub
Private Sub DestroyMyTestClassList()
' Does anyone know what code to put here, code that would totally destroy
' the MyTestClass and the 5 million items in it, reallocate the memory used
' by the list Of(T) MyTestClass
' MyTestClass.Clear() ' Doesn't work ??
' MyTestClass = Nothing ' Doesn't work ?
End Sub
Public Class TestClass
Public Property Value1 As String
Public Property Value2 As String
End Class
End Class
我理解让它超出范围的想法,但我必须告诉你,我尝试处理表单,关闭它等并加载单独的表单,但内存仍然没有回来。我猜 GC 还没有处理它的收集业务或其他什么。我想我得再看 GC 的工作时间
这位名叫 Jods 的家伙很好奇我为什么要以编程方式执行此操作。我正在做的工作类型是创建大量类型的自定义列表,这些列表本质上是非常动态的,我需要一种方法来销毁它们并重建它们,确保旧的东西已经消失了。这些应用程序有时会连续运行数周。. 我需要密切注意记忆和诸如此类的东西。
如果有人感兴趣,请参阅下面的代码,该代码似乎从对象列表中重新分配内存
Public Class List_Of_T_Test
Private MyTestClass As New List(Of TestClass)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Const LIST_COUNT As Integer = 5000000
Dim Count As Integer = 1
Do Until Count > LIST_COUNT ' 5 million iterations
MyTestClass.Add(New TestClass With {.Value1 = CStr(Count), .Value2 = CStr(Count + 1)})
Count = Count + 1
Loop
MsgBox(MyTestClass.Count.ToString)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' This seems to completely reallocate the memory, paste this into a form's code,
' add two buttons, click button1 once, or few times while watching the
' 'Windows Task Manager' PF Usage.
' Then Click button2 once and the memory spike levels off, it all comes back
MyTestClass = New List(Of TestClass)
GC.Collect() ' ?
End Sub
Private Class TestClass
Public Property Value1 As String
Public Property Value2 As String
End Class
End Class