我从自定义类中有一个数组的内存泄漏,我该如何清除/" dispose"它?

时间:2015-02-20 17:34:05

标签: .net vb.net ants

我有一个对象被保存在内存中。对象是“Trigram()”

这是少数字符串;

tmodels("en") = New String() {" th", "the"}

然后像这样使用

For Each key As String In tmodels.Keys
        Dim t As String() = DirectCast(tmodels(key), String())
        Models(key) = New TrigramModel(t)
    Next
    'release temp data structure
    tmodels = Nothing

冒着张贴太多的风险,整个TrigramModel类都在下面。

 Public Class TrigramModel

    Private m_trigrams As Trigram()
    Public ReadOnly Property Trigrams() As Trigram()
        Get
            Return m_trigrams
        End Get
    End Property

    Public Sub New(ByVal trigramsAndCounts As Hashtable)
        Dim keys2 As New List(Of String)()
        Dim scores2 As New List(Of Integer)()
        'convert hashtable to arrays
        For Each key As String In trigramsAndCounts.Keys
            keys2.Add(key)
            scores2.Add(CInt(trigramsAndCounts(key)))
        Next

        Dim keys As String() = keys2.ToArray()
        Dim scores As Integer() = scores2.ToArray()

        ' sort array results
        Array.Sort(scores, keys)
        Array.Reverse(keys)
        Array.Reverse(scores)

        'build final array
        Dim result As New List(Of Trigram)()
        For x As Integer = 0 To keys.Length - 1
            result.Add(New Trigram(keys(x), scores(x)))
        Next


        m_trigrams = result.ToArray()
    End Sub

    Public Sub New(ByVal tgrams As String())
        Dim keys2 As New List(Of String)()
        Dim scores2 As New List(Of Integer)()
        'convert hashtable to arrays
        Dim score As Integer = 0
        For Each key As String In tgrams
            keys2.Add(key)
            scores2.Add(System.Math.Max(System.Threading.Interlocked.Increment(score), score - 1))
        Next

        Dim keys As String() = keys2.ToArray()
        Dim scores As Integer() = scores2.ToArray()

        ' sort array results
        Array.Sort(scores, keys)
        Array.Reverse(keys)
        Array.Reverse(scores)

        'build final array
        Dim result As New List(Of Trigram)()
        For x As Integer = 0 To keys.Length - 1
            result.Add(New Trigram(keys(x), scores(x)))
        Next


        m_trigrams = result.ToArray()
    End Sub

    Public Function HasTrigram(ByVal trigram As String) As Boolean
        For Each t As Trigram In m_trigrams
            If t.t = trigram Then
                Return True
            End If
        Next
        Return False
    End Function

    Public Function GetScore(ByVal trigram As String) As Integer
        For Each t As Trigram In m_trigrams
            If t.t = trigram Then
                Return t.score
            End If
        Next
        Throw New Exception((Convert.ToString("No score found for '") & trigram) + "'")
    End Function


End Class

Public Class Trigram

    Public t As String = Nothing
    Public score As Integer = 0
    Public Sub New(ByVal t As String, ByVal s As Integer)
        Me.t = t
        score = s
    End Sub

End Class

我如何处理Trigramm()或您在本课程中看到的任何其他问题?

1 个答案:

答案 0 :(得分:0)

不建议您这样做。但是你可以强制垃圾收集来查看正在发生的事情,或者测试你的理论。

GC.Collect()
GC.WaitForFullGCComplete()

正如我所发现的,在极少数情况下使用它可能是一个好主意。