Forum Discussion

sastowe's avatar
sastowe
Super Contributor
12 years ago

vbscript and garbage collection

I am in the habit of setting object variables to nothing when I am done with them. Somewhere in my past, I heard that the microsoft scripting engine had in-determinant timing of their garbage collection. Unlike VB6, garbage collection was done who only knew when. So we cannot count on those resources being recovered when the variables go out of scope? Is my memory making stuff up? Is this true or bogus? What do all y'all do? Thanks



S

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Stephanie,



    VBScript releases resources when their reference count becomes zero. When the variable goes out of scope, it is released by VBScript's engine which results in decreasing the corresponding object reference count by one is this variable referenced some object.

    The above means that that explicit setting variables to Nothing is a) a good programming style (though Eric in the referenced article argues with that) and b) is required when you need to release the instantiated object before the variable that references it goes out of scope.
    • ray_mosley's avatar
      ray_mosley
      Frequent Contributor

      What about garbage collection with TestComplete itself and project variables?

       

      That is, if I allocate a project variable and associate a value, use it, and then remove that project variable, is the memory for that project variable released immediately, garbaged collected at some arbitraty time, or remains until the project executions ends?

      • Manfred_F's avatar
        Manfred_F
        Regular Contributor

        warning:

        If You create an object variable out of a script Extension and store it in a native TC vbscript module as a global variable, You will not get rid of it before reloading the scripe Extension or closing TC.

        Do Not Use vbscript here. Use JavaScript, which has got a CollectGarbage Statement.