Re: Memory not freed when unloading forms

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ralph (nt_consulting32_at_hotmail.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 16:01:33 -0600


"Leonardo Bosi" <unreal@address.com> wrote in message
news:ey%23wTQ%239DHA.2644@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> I have a VB6 application with several forms. The main form acts as a
> "dispatcher", it has various command buttons placed on it, each one to
> invoke a particular form in the application.
> The code behind these buttons launches the particular form in a 'modeless'
> fashion, referring to the form by its name (for instance if the form's
name
> is Form1, the code will be Form1.Show) This causes the form effect: if the
> form is not loaded at that time, it loads and then displays as the
> foreground window. If the form is already loaded at that time, that
instance
> of the form is displayed as the foreground window. This is fine, because
> it's exactly the intended form behavior for the application.
> What's not fine is the severe problem we are experiencing: memory doesn't
> seem to be freed when a Form is unloaded. All forms have a Cancel command
> button which issues the 'Unload Me' statement and causes the form to
unload.
> Since we've been experiencing problems which seemed to be 'random' with
our
> application, we did this test: put our application to run and opened a
> program to monitor the global amount of free memory. When we opened a form
> in our application, free memory decreased a little, let's say 7%. But when
> we closed that form, free memory didn't increased by 7% but only a small
> amount of the original decrease, let's say 2%. Opening the form again
would
> result in another 7% decrease, and closing it would yield a 2% increase.
> After several iterations of the experiment, free memory eventually reached
> 0% and the application began to experience the errors we had perceived as
> random at first.
> Finally we closed our application and suddenly all the memory it had been
> using incrementally was freed at once, this way restoring the free memory
> level as it was before running our app. Our application 'exit' command
loops
> through the forms collection unloading every form which is loaded at that
> time, except for the current one (the main form). After the loop, it
unloads
> the main form. This seems to be a well-behaved code which results in
memory
> being correctly freed when the application shuts down.
> As you can see, the problem we have is that we are unable to free memory
> correctly after unloading a form. We searched the MSDN library and
> Microsoft's Knowledge Base for information on how to do this, but didn't
> found anything useful yet. We tried alternatives in code such as Set Form1
=
> Nothing after unloading it, but didn't work either.
>
> Has anyone ever experienced this kind of problem?
> Any ideas about how to solve it, or things to try?
>
> Any help would be greatly appreciated.
>
> Thanks in advance,
>
>
> --
> Leonardo Bosi
> Buenos Aires, Argentina
>

Leonardo,

There are two interesting 'features' about Forms that I believe may be part
of the problem.
1) Unloading a form using 'Unload' only unloads the GUI portion of a form.
The code portion is not unloaded. So not only are 'globals' in these forms
still set to any values (perhaps still referencing another resource or
another form [see 2 below]), but the form is still partially in memory.
So you should not only ...
     Unload Form1
     ' to unload the form and
     Set Form1 = Nothing
      ' to unload the rest and de-initialize any references.

To be really paranoid (and safer) use the
    Dim frm1 As Form1
    Set frm1 = New Form1
this makes it a little easier (imho) to keep all your references herded
together...e.g.
once you have set frm1 to Nothing any attempt to use frm1 will create an
error - not so if using the name "Form1".

2) Referencing a form that is not loaded causes that form to be loaded
automatically.

Also, how are you checking your virtual memory use? You may have to stress
test your application under a load to see real results. Win98 virtual memory
is a little 'lazier' than say Win2k/XP. It is not uncommon for many pieces
to stick around in memory until that space is needed. That is why you will
see an app take say 30% of available memory when running alone, but only
take around 15% when other apps are loaded.

But I really think you basic problem is not completely unloading forms in
the first place and leaving references open.

HTH
-ralph



Relevant Pages

  • Re: Memory not freed when unloading forms
    ... Regarding how do I check virtual memory usage, ... When we opened a form>> in our application, free memory decreased a little, let's say 7%. ... >> through the forms collection unloading every form which is loaded at that>> time, except for the current one. ... > ' to unload the rest and de-initialize any references. ...
    (microsoft.public.vb.general.discussion)
  • Re: Compiler and How It Handles Scope
    ... All variables, whether Private or Public, are allocated relative addresses ... in that aggregate data area. ... in lieu of tying up memory with a different name and value. ... > ways they can be prevented from unloading. ...
    (microsoft.public.vb.winapi)
  • Re: writing overlays with VB6
    ... As far as I know unloading a form will not completely free up memory from ... I know that this could be done with DLLs, ... Using a Form Reference and specifically 'newing' and setting to 'nothing' ...
    (microsoft.public.vb.winapi)
  • Re: Memory not freed when unloading forms
    ... > What's not fine is the severe problem we are experiencing: memory doesn't ... > in our application, free memory decreased a little, let's say 7%. ... > correctly after unloading a form. ...
    (microsoft.public.vb.general.discussion)
  • Memory not freed when unloading forms
    ... All forms have a Cancel command ... Since we've been experiencing problems which seemed to be 'random' with our ... in our application, free memory decreased a little, let's say 7%. ... correctly after unloading a form. ...
    (microsoft.public.vb.general.discussion)