CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2001
    Location
    CA
    Posts
    153

    Deallocation of variable memory

    I can't seem to get a good answer to these questions:
    When do you need to set objects to Nothing?
    When do you need to Unload forms?
    When do you need to Erase Arrays?

    The MSDN documentation seems to be conflicting:
    "Each object uses memory and system resources. It is good programming practice to release these resources when you are no longer using an object.
    Use Unload to unload a form or control from memory.
    Use Nothing to release resources used by an object variable. Assign Nothing to an object variable with the Set statement."
    AND
    "All object variables are automatically cleared when they go out of scope."

    Which of these is true? Does anyone know of a good source for a clear cut explanation of VB Memory managment?


    thanx/good luck,
    adam
    thanx/good luck

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Deallocation of variable memory

    Well, I don't have all the answers but maybe I can help a bit. Lets look at the code of a typical form.


    option Explicit

    ' uses mem as long as form is loaded
    private X as Long
    private myWS as Workspace
    private myDB as Database
    private myRS as Recordset

    private Sub Form_Load()

    ' myWS, myDB and myRS gets mem space
    ' This memory will be in use as long as the form
    ' is loaded or until they are explicitly set to nothing
    ' X declared as long will have mem space as long as
    ' the form is loaded. You can't set it to nothing
    X = 100
    set myWS = CreateWorkspace("", "admin", "", dbUseJet)
    set myDB = myWS.OpenDatabase("my.mdb", false)
    set myRS = myDB.OpenRecordset("SELECT * FROM Client;", dbOpenDynaset, dbPessimistic)

    End Sub

    private Sub cmdCreateTmpRS_Click()
    Dim myRS1 as Recordset

    ' myRS1 starts using memory here when
    ' we set it to an existing recordset.
    set myRS1 = myRS
    myRS1.MoveLast
    myRS1.Close

    ' Here, since myRS1 is declared as a local object
    ' it goes out of scope and no longer uses memory.
    End Sub

    private Sub CmdClearRSMem_Click()
    ' I starts using memory here
    Dim I as Integer

    myRS.Close
    for I = 1 to 10
    next I
    ' here myRS is unloaded from memory. It won't use
    ' mem again until you set it to something else
    set myRS = nothing

    ' here I goes out of scope and is cleared from memory
    End Sub

    private Sub Form_QueryUnload(Cancel as Integer, UnloadMode as Integer)

    ' here we are explicitly clearing the recordset
    ' and the database from memory
    set myRS = nothing
    set myDB = nothing
    ' here, we are unloading the form, myWS goes out of
    ' scope and is cleared from memory
    ' also the form goes out of scope so any memory it was
    ' using is cleared.
    End Sub





    So lets review. Any variable/object declared within a Subroutine or function goes out of scope when you exit the sub/function and is cleared from memory.

    Any variable/object declared within the form is used the entire time the form is open. When the form is unload, it to goes out of scope and is cleared from memory.

    At anytime, you can explicitly clear an object from memory by setting it to nothing.

    At anytime, you can explicitly clear a control/form from memory by unloading it.



    As for the good source for a good explantion, I can't help you there.

    Hope this helps.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured