Library tutorials & articles

Performance Issues

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.

Comments

  1. 29 Sep 2005 at 20:20

    And once again a greate article by james!

  2. 21 Mar 2002 at 09:09

    This posting is specifically a response to James Cowleys suggestion that using ByVal is quicker than using ByRef.  This is only true when calling an out-of-process server (i.e. and ActiveX EXE).  VB is designed to pass all parameters ByRef, which means that everything is passed as a 32-bit pointer.  When passing ByVal VB copies the contents of the parameter into temporary space and then passes a 32-bit pointer to the temporary space.  This means that, counter-intuitively, it is slower passing a long ByVal than it is ByRef.  
    When calling an out-of-process server OLE must marshal a copy of your parameter into the address space of the routine you are calling and then, if it is ByRef, copy it back afterwards.  This is probably the only occasion that passing ByRef is slower.
    Generally ByVal should be reserved for occasions when the routine is going to change the contents of the variable and the calling routine will be affected by the change.  At the very least use ByRef for all strings and variants.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Performance Issues.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!