Ok, thanks to some users (@jmcilhinney, @Anu6is) who spent lot of time helping me to learn, i post my answer.
The correct syntax to release local resources in a method is this:
Function GetString() As String
Try()
Dim sb As New StringBuilder
sb.AppendLine("my first line")
Return sb.ToString()
Catch()
Return Nothing
Finally()
'N.B. Good practice don't write
'sb = Nothing
End Try()
End Function
Please, note that in the Finally statement i wrote 'Good Practice' to don't write
sb = Nothing
Why?
Because every Local variable inside of a method, when the execution of the code exit the method, are automatically 'cleaned' by the Garbage Collector (the GC release the memory previously occupied by the local variables, but if the execution of the code will use again the method before the garbage collector runs, the memory area previously occupied will be riallocated by the pointer of the new instances of the local variables).
Different approach is when a method uses a non-local variable:
Class MyClass
Dim sb As New StringBuilder
Friend Function GetString(ByVal text as String()) As String
Try()
For Each Txt As String In text
sb.AppendLine(Txt)
Next Txt
Return sb.ToString
Catch()
Return Nothing
Finally()
'Here i can write:
'sb.Clear
'or
'sb = Nothing
End Try()
End Function
End Class
Why in this case could be a good practice to set the variable to Nothing or (in this case) release the memory occupied by the StringBuilder Value?
Could be, if in example, i use the method in the beginning of the program, and not more.
Or if the StringBuilder 'store' an huge amount of data, for lot of time.
In this case, we 'choose' to set the StringBuilder to Nothing, to release occupied memory, that will not be used by the program.
This article, explain well the cases when, it's good practice to set a variable to Nothing, inside of a method.
https://blog.stephencleary.com/2010/02/q-should-i-set-variables-to-null-to.html
After this, a question could come in mind:
'But if i wait that the Garbage Collector will 'release' memory, for how long i have to wait? ...Ticks ...Milliseconds, ...Seconds, ...Minutes...?
Here a link that can help:
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals