我有这两个版本的代码(做同样的事情):
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()
答案 0 :(得分:0)
因此,当这样编写时,它将起作用:
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