代码之家  ›  专栏  ›  技术社区  ›  CruleD

方法完成后如何清除JArray使用的内存

  •  -2
  • CruleD  · 技术社区  · 7 年前

    我有两个版本的代码(做同样的事情):

    Public List As New List(Of String)
    Private Sub ReadList()
        Dim ListJSON As JArray = JArray.Parse(My.Resources.list) 'List in json format
        For Each item As JObject In ListJSON
            List.Add(item("name").ToString)
        Next
    End Sub
    

    Public List As New List(Of String)
    Private Sub ReadList()
        List.AddRange(My.Resources.list2.Split({","}, StringSplitOptions.None)) 'List in string format item1,item2,...
    End Sub
    

    第一个版本使应用程序消耗1GB内存,而第二个版本仅消耗100MB内存。 如果我拍摄快照,在第一个版本完成后,它看起来与第二个版本几乎相同,但内存没有被清除。


    Dim ListJSON As JArray = JArray.Parse(My.Resources.list) 'List in json format
    

    有没有办法清除它使用的内存?我尝试了以下方法(但不起作用)。

    ListJSON = Nothing
    GC.Collect()
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   CruleD    7 年前

    因此,当这样写时,它是有效的:

    Public List As New List(Of String)
    Private Sub ReadList()
        Dim ListJSON As JArray = JArray.Parse(My.Resources.list) 'List in json format
        For Each item As JObject In ListJSON
            List.Add(item("name").ToString)
        Next
        ListJSON.RemoveAll()
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub