如何销毁List Of(T),重新分配列表使用的内存

时间:2013-07-22 17:43:02

标签: vb.net visual-studio-2010 .net-3.5

VB.NET 2010,Framework 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

2 个答案:

答案 0 :(得分:2)

我很好奇为什么你需要一种方法来编程。当您的对象不再使用时(所有引用都不可访问/超出范围/或设置为null),下一次运行垃圾收集器将回收内存。

如果要按代码触发集合,则应调用GC.Collect()。如果你有终结器,你可能需要调用GC.WaitForPendingFinalizers()然后再调用GC.Collect()。

请注意,调用这些方法通常都不赞成,所以我很好奇你的情况。

答案 1 :(得分:-1)

当.NET框架实例化一个对象时,它会在托管堆上为该对象分配内存。该对象保留在堆上,直到它不再被任何活动代码引用,此时它所使用的内存为“垃圾”,已准备好由.NET垃圾收集器(GC)进行内存释放。在GC释放内存之前,框架调用对象的Finalize()方法,但开发人员负责调用Dispose()方法

因此,只需调用对象的Dispose()方法。