Question about DIM statements

Posted on 1999-12-21
Medium Priority
Last Modified: 2010-05-02
If I have a DIM statement inside a timer or a frequently used subroutine, does that re-DIM the variable each time? And does that make my app use up resources that it shouldn't? That's my problem. My app runs a while and the machines run out of resources.
What other things can I adjust or fix to help minimize this problem.?
Question by:rbend
LVL 32

Expert Comment

ID: 2299370
You can Dim within any function, it does not take up more memory every time the function is entered.

There are lots of things which may be causing a memory leak.  What are you doing in the timer?  Are there any other repetitive tasks which may be causing the problem?

Accepted Solution

Jon_Raymond earned 150 total points
ID: 2299430
If you are dimming any OBJECTS you have to make sure you release them at the close of the procedure, or in your case maybe when you stop using them.  This is a common mistake that some developers make.  For example if oWord were your object:

Dim oWord as Word.Application
Set oWord = New Word.Application  

' do so other stuff
' then exit:

Set oWord = Nothing

In this example if you don't quit word and set it equal to nothing then you will create a new instance of word every time you go through the procedure.  If you were to check the Windows task manager you would see mutiple instances of word running as separate applications.


Expert Comment

ID: 2299670
I'm not trying to answer this question, because both Erick37 & Jon Raymond have already done so, but I thought rbend could use a bit more of an explanation.

A procedure-level variable declared with the DIM statement retains its value until the procedure is finished running. If the procedure calls other procedures, the variable retains its value while those procedures are running as well.

If a procedure-level variable is declared with the STATIC keyword, the variable retains its value as long as code is running in any module. When all code has finished running, the variable loses scope and its value. Its lifetime is the same as a module-level variable.

A module-level variable differs from a static variable. In a standard module or a class module, it retains its value until you stop running your code. In a class module, it retains its value as long as an instance of the class exists. Module-level variables consume memory until you reset their values.

Variables dimensioned as object pointers are supposed to release memory in the same way, however, that is not always the case. Memory leakage can occur, so as Jon recommends, set the object variable to NOTHING as you exit your procedure.

Expert Comment

ID: 2299733
By the way, If your timer event procedure takes longer than the timer period, you'll be nesting event calls. That'll earn you a stack overflow. Try commenting out your timer procedure & see if that fixes the resource problem. If it does, you know what to do.

Author Comment

ID: 2300346
thanks all of your for your time !
Happy Holidays

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question