Click to See Complete Forum and Search --> : Form Load and UnLoad - MEMORY RELEASE


M.Anand
March 25th, 1999, 09:07 AM
I Load a Form on Menu Click. Some amount of memory is consumed ( let us say 400 k). When I close the form memory is not released . When I load the form again, around 4 or 8K of extra memory is consumed and the form is loaded. So the net result is


Mem Usage 1st Time -- 400K

2nd Time -- 408K

3rd Time -- 416K,etc...


This happens for all the forms.


How do i forcibly release memory ??


Anand

Chris Eastwood
March 25th, 1999, 09:13 AM
Hi


VB actually keeps forms around in memory - even if you explicitly 'unload' them - this is due to any static variables or routines you may have.


If you want to remove them from memory completely, use the following syntax :


Unload frmWhatever

Set frmWhatever = Nothing ' this releases the memory Code Segment


Also, if you are referencing multiple instances of forms in your code, make sure that you **Never** do a 'Dim frmMine As New frmWhatever' - this will load the form (or check if it's loaded) on each reference to the variable, also, once you've unloaded the form - it will load again on the next property access -


eg.


Dim frmMine As New frmWhatever


frmMine.Show


Unload frmMine

Set frmMine = Nothing ' Won't set it to nothing


frmMin.Show ' will reload it again


Regards


Chris Eastwood


CodeGuru - the website for developers

http://www.codeguru.com/vb

M.Anand
March 25th, 1999, 09:21 AM
Hi Chris


I have done that already ( Set form = Nothing).


I have loaded Form1.

On exit button of the form I call Unload Form1.

On Form1_UnLoad event I do Set Form1 = Nothing.


Actually while I am writing this mail,Windows released around 2 MB when my project was running !!!


Let me tell what I do when I load a form. I open a Table in the database and do a read

operation. But I close the recordset and set it to Nothing


Anand

Chris Eastwood
March 25th, 1999, 09:27 AM
Have you tried changing the :


Unload Form1

Set Form1 = Nothing


in the Form1 code to using the 'Me' keyword ?


Actually, I doubt that using 'Set Form = Nothing' in the Form's Unload procedure would do anything - the Form is still loaded afterall, even if it is unloading at that point.


VB Cannot clear down any memory held by your form if it's still in the unloading stage.


How are you handling the creation of your forms ? I take it they are modeless (ie. you can open many of them at one time).


You may need some kind of unload-notification code which can tell your main form to set the references to the existing form to nothing.


Regards


Chris Eastwood


CodeGuru - the website for developers

http://www.codeguru.com/vb

M.Anand
March 25th, 1999, 09:32 AM
Chris


Most of the forms are Modeless since I am handling some parallel events through RPC calls


Anand

Ravi Kiran
March 25th, 1999, 07:59 PM
Hi Anand,


I hope i got your problem correct : On Form1's menu item select, you are showing form2, in a modeless fashion. Try something like this:


In Form1 ( ie, your main or base form):

Dim m_lpForm2 as Form2


In menu item event handler,

private sub menuitem1_click()


set m_lpForm2 = New Form2

m_lpForm2.Show VbModeless ', Me ' You may like to add Me also here?

...

end sub


Have a Public member in Form2 that says the form is unloaded

In form 2:

public MyUnloadState as Boolean


In Form_load of Form2 : say

MyUnloadState = False

In Form_Unload of Form2 , use

MyUnloadState = True


In form1, when you feel the work with form2 is over, check this public variable and if true set the instance to Nothing . i.e

...

'' at some approp time:

if m_lpForm2.MyUnloadState = True then

' checking a public variable doesnot load a form, even if not loaded,

' as Chris was pointing out, this is the cause for memory lockup.

Set m_lpForm2 = Nothing

end if

'' Deciding what is the appropriate time is based on the problem at hand!


If you want multiple instances of Form2 to exist simultaneously, then you may want to use a array or collection for m_lpForm2


I hope it helps.


Ravi