Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Re: Memory Utilization & variables


Message #1 by "Gregory A. Beamer" <gbworld@h...> on Thu, 11 May 2000 4:21:34
On 04/28/00, ""Kevin Riggs" <kevin.riggs@p...>" wrote:
> I've read several places that global variables, while useful, can also eat
memory and possibly cause a noticable depreciation in speed.  After 
working
to optimize all my latest code to keep global variables to a minimum, I 
realized that all my local variables that were created when a form is 
Loaded are also in memory.

This depends on what you are calling local. If the variables you are 
talking about are form level, then they are module level variables. Local 
variables are those that are loaded into subroutines, functions, methods, 
etc. These will be in memory when you are completing actions on your form, 
but will fall out of scope when the work is done. If you are talking about 
objects, they will be released when set to nothing.

>Am I shooting myself in the foot by moving variables that are only loaded
seldom onto forms that I am using somewhat frequently?  If the variable is
going to be populated once on average per use of the form, should I really
be loading it into memory when the application starts and I load the most
commonly used forms?

Yes! No! In other words, you really need to test the application. The 
danger of global variables is that they can be overwritten with other 
copies of a form. In addition, they are always in memory. If you load the 
form and can only have one open at one time, and have form variables set at 
module level, then it may be better to be at global level.

>What is the impact of moving the variables to global constants?  Do I 
save
any memory?  How do I declare a constant globally?  In my global module 
(gMod)?

If you are truly talking constants you are not saying the same thing as 
variables. When I think variables, I think of values that can be changed. 
IF you need to set global variables or constants, do put them in a .bas 
module so they can easily be found during program maintenance. You can 
declare like so:

Global Const foo as String = "x"
Global foo as String

However, this is a deprecated keyword, so using Public is more acceptable. 
You may still find some older code with Global, but I would not count on it 
always being a part of the language, so use "Public" in your mod instead.

As with all things, test...test...test! The rules work most of the time, 
but are not 100%. In MTS, for example, we found changing from early to late 
binding often performed over 300% better than early binding; the rules 
(textbook) would suggest the opposite would be true.

GB


  Return to Index