DOCUMENT:Q185990 21-MAY-1998 [vbwin] TITLE :Improving Performance of Object Deallocation PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0 OPER/SYS:WINDOWS KEYWORDS:VBKBObj ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0 - Microsoft Visual Basic Standard, Professional, and Enterprise Editions for Windows, version 4.0 --------------------------------------------------------------------- SUMMARY ======= By default, object deallocation in arrays and collections can be inefficient when there is a large number of objects. This article describes a technique for improving deallocation performance. MORE INFORMATION ================ When deallocating a large number of objects in a collection or an array, performance of the deallocation process can be improved if objects are deallocated in the reverse order of their creation--this results in linear performance. For arrays of objects this is simple; however, for collections it can be difficult to determine the allocation order. In this case, the following code provides you with good to reasonable performance: Public Sub DeallocCollection(colSource as Collection) Dim lIndex as Long Dim lMax as Long lMax = colSource.Count ReDim aryLocal(lMax) as object For lIndex = 1 to lMax if isObject(colSource.index(1)) then _ Set aryLocal(lIndex) = colSource.index(1) colSource.Remove(1) Next set colSource = nothing For lIndex = lMax to 1 step -1 set aryLocal(lIndex) = Nothing Next End Sub The performance improvement depends upon the level of fragmentation in the collection. Additional query words: kbDSupport kbdss vb6rel VBKBObj ====================================================================== Keywords : VBKBObj Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbinfo ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.