开发者

Is it better to destroy all objects or just let the garbage collector do the job?

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 a Using statement. But there's still no reason to set its reference to Nothing.


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 call Dispose itself when it is being finalized (destroyed, freed), but objects are not required to do your work for you.

See also

  • Proper use if IDisposable


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜