VB Garbage collection

This article claims it’s pointless to Set objects to Nothing in VB when you’re finished with them, except in particular cases. Maybe it’s those particular cases I’ve come up against, but I don’t believe that, myself. I’ve observed time and time again IIS apps that call VB6 DLLs grabbing more and more memory for DLLHost, and eventually choking until an iisreset has been done.

Going through the code (both the ASP VB Script and the underlying DLL VB) with a fine tooth comb and ensuring EVERY object got set to Nothing afterwards cleared these issues. Though I’m embarassed to admit that on one early project I worked on, a bunch of people looked through the code trying to find the leak, couldn’t find it, and someone eventually wrote a nightly iisreset batch job.

Reading that article, it occurs to me that maybe there was a case of destroying objects that referred to each other in the wrong order. The code is long sinced vanished, so I don’t know, and don’t care to find out.

As far as I’m concerned, it’s not only better for performance to destroy your objects, and a helluva lot easier to do all the time than just some of the time. It’s also tidier in the code. I’ll keep on doing it.

.Net, of course, is a different kettle of fish altogether.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

11 thoughts on “VB Garbage collection

  1. Firefight

    If the VB runtime doesn’t handle garbage collection properly then setting your local variables to Nothing isn’t going to help any. What you are proposing is superstitious nonsense.

  2. Augusto

    I have a VB6 application which’s memory increases from 190 MB to 500 MB, time to time.

    No memory leaking, as all objects are set to Nothing after usage.

    So, VB6 DOES something weird with memory.

  3. George

    Completely agree with daniel and Augusto, have recently been assigned to sifting through thousands of lines of code to try and find out why VB is refusing to release objects and my investigation has revealed that VB’s ‘Garbage Collector’ is no better than the London Underground during a tube strike

  4. Jaoquim

    but i still with same question:(
    how can i dispose/destroy 1 object in VB6?
    i read very things, but without sucess 🙁
    everything thta i read tell me that i need close every resources, but i still confuse:(
    can anyone help me?
    thanks

  5. daniel Post author

    set x = nothing

    But as noted in the post, you have to do it in the right order if you have multiple objects referring to each other.

  6. Jaoquim

    what i did was:
    create 2 commandbuttons in form(in project mode).
    then:

    Private Sub Command1_Click()
    Set Command2 = Nothing
    End Sub

    but i recive 1 error message:
    “Compiler error:
    Invalid use of property”
    what you mean by these:
    “But as noted in the post, you have to do it in the right order if you have multiple objects referring to each other.”?
    i will tell you what i’m doing:
    i’m build 2DSprite control, is 1 object for create games easely and quickly. is why i need put the Destroy method in object. or if “set x=nothing”
    worked, then perfect… if not i still in bad situation(using the visible property for test everything):(.
    if you what, put me in you MSN(if you have it ;)): Cambalinho_83@hotmail.com
    thanks for help me

  7. daniel Post author

    You can’t destroy controls on a form like that.

    Sorry, I can’t help further, don’t have time. Suggest you look for the question on stackoverflow.com and if it’s not there, ask it.

Comments are closed.