I have a function like the following:
Public Function testFunction(ByVal input_string As String) As String
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To input_string.Length - 1
c = input_string.Chars(i)
s.Append(c)
Next
Return s.ToString
End Function
but I want know if it's better to explicitly destroy any object, like this:
Public Function testFunction(ByVal input_string As String) As String
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To input_string.Length - 1
c = input_string.Chars(i)
s.Append(c)
Next
Dim t As String = s.ToString
s = Nothing
Return t
End Function
or just let the garbage collector do the job for us?
Both the above functions work, but I want only know the best practice for performance...
thank you
Setting
s = Nothing
is pointless as it falls out of scope at the very next instruction thus becoming eligible for garbage collection at that point anyway.Setting a variable to null/nothing doesn't mean the garbage collection kicks in at that point.
It's always better to let the garbage collector do the job for you. Think about it like this: a programmer much smarter than you (and I) carefully optimized the garbage collection routine to make life much simpler and painless for you. It's unlikely that we can do a better job ourselves, and more likely that we'll just interfere.
Basically, objects that are instantiated as local variables will become eligible for garbage collection as soon as they fall out of scope (i.e., that method finishes execution). Your
StringBuilder
object is going to be garbage collected whenever the garbage collector runs (which is another thing you don't have to worry about—there is no way to know the next time the GC will run).Setting an object to
Nothing
literally does nothing (note that this is different than pre-.NET versions of Visual Basic). It will generally be optimized out by the compiler in Release mode, and even if it isn't, it's not helping you in the least.However, if an object has a
Dispose
method, you should be calling it. Or better yet, wrap it in aUsing
statement. But there's still no reason to set its reference toNothing
.Let the garbage collector do this. Classes that implement the IDisposable interface are designed for you to manage pre-garbage collector - meaning that you invoke object.Dispose(). This is because those classes use limited system resources like file handles and GDI brushes. If it doesn't implement IDisposable then its usually safe to just let the garbage collector handle it.
If an object doesn't implement
IDisposable
then you are correct to do nothing (you couldn't free the object if you wanted to).If an object implements
IDisposble
you must call.Dispose()
- you are using it wrong if you don't. The object can try to protect you from yourself, and callDispose
itself when it is being finalized (destroyed, freed), but objects are not required to do your work for you.See also
The whole point of garbage collection is so that you don't have to manual manage memory anymore. Let the garbage collector do its job.
Additionally, setting a variable to
Nothing
does not mean that the GC is going to kick in and free the memory; the GC is non-deterministic. It could run immediately, it could run in four score and seven years.The objects there are all going to get destroyed when the code ends the function, as the variables are out of scope. The
s = Nothing
is redundant because on the next run of the GC, those objects will be destroyed anyway.