NathanX
NathanX

Reputation: 145

Visual Basic garbage collection

Is it still useful in VB.NET to assign objects to Nothing when finished using them, as mentioned here? Or has garbage collection improved to the point that this is no longer helpful/necessary?

Upvotes: 3

Views: 2778

Answers (3)

Raphael R.
Raphael R.

Reputation: 24274

You are right, it's no longer needed except in a few corner-cases:

  • To eliminate cyclic references (Object A references Object B which references Object A) Works fine, see comments.
  • If you're working with VBScript. Set those object to Nothing.

Upvotes: 1

dario_ramos
dario_ramos

Reputation: 7309

As the others said, it's not necessary in most cases.

If you are done using an object and want to claim its memory as soon as possible (for example, because it's a very big entity which contains many others), make it implement the Disposable pattern and use it via the Using directive.

In the particular case in which your big object does not reference any unmanaged resource, this is not fixing a memory leak, it's keeping your memory footprint small

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

No, it's not useful in VB.NET. IIRC you should do this only if variables are pointing to dynamically created COM objects such as if you are doing Office Interop for example.

Upvotes: 1

Related Questions