Microsoft & .NETVisual BasicVB Coding Tip: Unload a Program Releasing All Memory

VB Coding Tip: Unload a Program Releasing All Memory

When you first start to program, memory management is not really an issue. But as you build larger and more powerful programs that are going to be used by more people, you will soon realise that your program needs to use the least amount of memory as possible, and, as a must, release it all after if finishes. This tip loops through the forms in your project unloading them. You might also want to consider setting your objects to nothing. E.g. Set db = Nothing

Private Sub Form_Unload(Cancel As Integer) 
Dim intCount As Integer 
While Forms.Count > 1 
'// Find first form besides "me" to unload 
intCount = 0 

While Forms(intCount).Caption = Me.Caption 
intCount = intCount + 1 
Wend 

Unload Forms(intCount) 
Wend 

'// Last thing to be done... 
Unload Me 
End 
End Sub 

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories