Vb. Net Syntax Using Stringbuilder in Function

Vb. Net Syntax Using Stringbuilder in Function

What is the correct code syntax (Set the StringBuilder instance to Nothing at the end the function) when use stringbuilder in a function that return a string? Usually i try to use the statement 'using', but StringBuilder don't implement iDisposable.

To be more precise: i want to release the memory occupied from the StringBuilder instance when GC will run, so i'd like to set the StringBuilder instance to Nothing at the end of the Function.

Please do someone know 'where' should i set the StringBuilder instance to Nothing?

Here's the code.

Function GetString() As String
    Try()
        Dim sb As New StringBuilder
        sb.AppendLine("my first line")
        Return sb.ToString()
        'Should i put here 'sb = Nothing' ?
    Catch()
        Return Nothing
    End Try()
End Function

Or it's better this solution?

Function GetString() As String
    Dim sb As New StringBuilder
    Try()
        sb.AppendLine("my first line")
        Return sb.ToString()
    Catch()
        Return Nothing
    End Try()
    'Or should i put here 'sb = Nothing' ?
End Function
21

2 Answers

I wrote the same function in c#, in your case is not necessary to clean the "stringbuilder" variable but, to make certain clean after the return statement you can use a try… finally block.

static string GetString()
{
     StringBuilder sb = new StringBuilder();
     try
     {
          sb.AppendLine("myfirstline");
          return sb.ToString();
     }
     finally
     {
          sb = null;
     }
}
3

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.

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:

Your Answer

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

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.