Memory
One disadvantage of visual basic is the lack of control over memory. If you
compile an empty project, and then run it, that program will use almost 2MB
of memory, without you having written a single line of code! This is due to
all VB programs requiring MSVBMV60.dll (or 50 etc depending on the vb version).
Unfortunately, there is not much you can do about the memory usage of this DLL.
However, there are a number of other ways.
1) Ensure that you are referencing only to files that you need. Click References
on the Project menu and see there are any you do not need. You will always need
the following items checked:
Visual Basic For Applications
Visual Basic runtime objects and procedures
Visual Basic objects and procedures
OLE Automation
2) Ensure that your program is only using the components it needs. If you are
using just one or two items from the Microsoft Common Controls, try to find
a seperate control (normally free) from another site. For example, we have a
Progress Bar & Hyperlink control, and VB
Accelerator has Image Lists, TreeViews, ListViews, Outlook bars and more!
This will cut down the size of the controls you have to distribute, as well
as the memory usage. Using any control from the Microsoft Common Controls requires
at least another 1MB of memory.
3) When your program is running, if a form is not likely to be needed again
quickly, use the Unload statement to remove the form from the memory. If you
use the Hide statement, it simply hides the form - it is still resident in the
memory. If you need the form, use the Load statement. This does take longer
than using the Show statement (only after you have shown the form once. The
first time the form is shown, using the Show statement, it also loads the form
into the memory), however, if your program is very memory hungry, it is one
way of cutting down its memory usage.
4) Put infrequently used code into forms or classes, which can be created and
destroyed as required. If you put it in a module, it will be loaded when your
program is first run, whether it is needed or not.
5) Use the right data type declarations. If you
have a variable that is only going to hold numbers 1 to 256, use the Byte data
type, rather than Integer or Long. This will use 1 byte of memory instead of
4 bytes. Declaring one variable as a byte instead of Long will not make much
difference, but if you have a large number of variables, it can help cut memory
usage down. Even if the effects are not noticeable, it is a good habit to get
into.
Have a performance tip that you would like mentioned? Email
us with it.