-1

I have these two versions of code (that do the same thing):

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

The first version makes the app consume 1GB of memory while second one only 100MB. If I take the snapshot, after first version finishes it looks pretty much the same as second one, but memory doesn't get cleared.


Edit: Well so since I know this line is causing the issue

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

Is there a way to clear up memory used by it? I tried the following (but it didn't work).

ListJSON = Nothing
GC.Collect()
7
  • 1
    They're doing very different things. Dec 2, 2018 at 15:36
  • @DaveNewton Fair, but the end result to the user it the same, a list of strings.
    – CruleD
    Dec 2, 2018 at 22:23
  • The point is that in the first version you’re creating en entirely new data structure, then creating a string from each item in the new data. When memory gets freed is up to the VM and whatever rules it uses to determine what’s available for GC, which when it comes to .NET, especially VB.NET, I don’t know—but if you already have strings, and know how to get what you want out of it, parsing it into objects will be a double-penalty in time and memory. Dec 2, 2018 at 22:28
  • @DaveNewton I don't, I created these manually to test because I didn't think there was a reason for a list of string to use that much memory.
    – CruleD
    Dec 2, 2018 at 22:30
  • It’s not the list of strings, it’s the list you’re getting the list of strings from. Dec 2, 2018 at 22:31

1 Answer 1

0

So it works when written like this:

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.