Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Out of Memory

Status
Not open for further replies.

huBBLe

Programmer
May 15, 2001
50
SG
Hello,

I have 10 forms with a lot of controls (abt 20 to 30 controls on each form). The forms will be shown in sequence, one by one, using a "Next" command button. When one form loads, the other is hidden, not unloaded, as I need the data for forms at the end that will require these data.

Now, I'm running a 1.1Ghz AMD K7 with 256 Mb RAM but I keep getting this "Out of Memory" error whenever I get to the 6th or 7th form. This shldn't be happening rite?

Any ideas why is this so?
 
Yeah. With that many forms sitting around it could happen. You will not like it but you should be saving that data in the QueryUnload Event into a .BAS, .CLS module or Scripting.Dictionary as I do and letting the forms disappear.
 
While I agree with John when he says that foms should disappear when not in use, 10 forms with 20 - 30 controls on each form is nowhere near enough to cause an out of memory error. One of the last projects that I worked on had over 175 forms and it ran on machines with only 64 MB Ram.
Normally, in VB, when you get an "Out of Memory" error it is because your are out of stack space. This can be caused by a recursive call that has gone on out of control . . . I have also seen some complex string manipulations cause this error.
Could you please post your code and so the exact line that the Out Of Memory happens on? - Jeff Marler B-)
 
I agree with JohnYingling. U can store the data into a temp media (txt file, or database file) and then unload all unneeded forms to clear some space in memory. Notice that if u named your controls on form without using index, this also has contribute on spending memory.
 
John,
In some case . . . yes all forms could be loaded into memory at the same time (trust me, the original design was not mine . . . I inherited the design during a conversion project). Even with that many forms, the application's memory consumption was low. the biggest memory strain we had for the process space was caused by a third party tool used to connect to the AS/400 (Written by Lansa) which increased the memory consumption by about 10 MB.
That is not to say, however, that I do not agree with you when you say that it is a good idea to unload Forms when they are not needed. I do agree with you and I think that it is just good design to destroy any resouce (be it a form or any other object) when it is no longer needed.
With that being said, I do not think that having 10 forms open with 20-30 controls each on a machine with 256 MB would cause an out of memory error. Unless of course there were several instances of form being instantiated at the same time.
I would still be interested in seeing the code and more specifically, the exact line that is throwing the error. Also, if the system this app is running on is an NT or 2000 box, it would be worthwhile to being up the task manager when the error occurs and see how much memory is actually being used by the specific process (both within the IDE and outside in its compiled format). - Jeff Marler B-)
 
Jeff,
Ok... but by "loaded" I mean Form_Load Event has happened and no Form_Unload event has occurred yet.

Total available memory makes no difference unless there is too litle. VB has a 1MB "limit" to stack space: from data limitations in the Programmer's guide
"Total available stack size for Visual Basic is one megabyte (1MB) per thread. A stack may grow beyond this, however, if there is adjacent free memory."
and I've seen somewhere that there is 2MB limit on a certain heap.

This whole "Windows Resources" thing seems unrelated to the amount of real or virtual memory. I can get the "90%.., Danger Will Robinson" message on a 192MB machine with 15GB available for paging. Some of it must be "real" memory that must be locked for video etc. I know from the mainframe that a "page fault" in the paging supervisor can not be tolerated so all that code must be locked down. Right now I'm sitting at 29% System Resources left with only 2 IEs, VB , MSDN, Zone ALarm open.

I mentioned the number of forms (and combo boxes seem to be a real user if resources) because a runaway recursion usually causes a stack overflow, unless you keep allocating a large dynamic array in each recursion.

I still feel that keeping an invisible form around just to hold data is not a good idea. I use the Scripting.Dictionary to hold that kind of data beacuse I need to reference it later and I NEVER access form controls outside of the form. I've heard that in VB.NET, all controls are PRIVATE to a form (with no option to make them PUBLIC?). Too many people think PUBLIC variables are a fantastic way to interface
 
Hi guys,

thanks for the many responses.

The line that throws this exception is juz a simple:

frmMyForm.Show

Now, on every form, i have a "Back" and "Next" button that allows the user to move thru the forms. The code for these two command buttons r juz:
***********************
Private Sub cmdBack_Click()
frmOtherInformation.Show
Me.Hide
End Sub
***********************
and
***********************
Private Sub cmdNext_Click()
frmPlanningProtection.Show
Me.Hide
End Sub
***********************

Note dat these two buttons show up on every form. So if u were to be at the last form, all the previous forms would be loaded into memory.

the form (frmMyForm) does not have any recursive calls, not even arrays. I remember dat i had the same problem wif another project but when i made the project .exe and used the Deployment wizard, the problem disappears. The program will run smoothly without ant hitches.

I have not tried out with this project, maybe it will go away when i make it .exe but that will not solve the root of the problem.
 
Juz compiled in .exe and tried running, but still got the out of memory error. does this mean i have to find a way to store the data from the previous forms if i were to need it at the end? it seems a tad silly dun u think to be running out of memory wif such little use of resources?

one thing i would like to point out is that vb will oso give me an out of memory error if i were to open too many forms (by double clicking on every form in the project browser). The error will occur at the 8th form, juz abt where the form dat causes the error will be loaded. This leads me to think that the problem does not lie in the form at all, rather, ANY form dat were to be loaded at that point would have caused this error.
 
huBBLle,
Is it the same form that causes the error every time? Or if you open the forms in a different order (either running or clicking on the forms in the IDE) will the error still occur on other forms? I am asking this in order to try and isolate where the problem is happening or if it is a global problem.
Just as a suggestion, if it is the same form everytime, I have seen cases where a bad setting on a form control can cause an out of memory error.
Also, as John said in his last post, keeping invisible forms around just to store data is generally a bad design idea. You could look at his scripting option that he uses. Another option is to design an object structure using classes and store the data there. Then when a new form is loaded, it just references the object structure and displays only what it has too. Currently, I primarialy work with n-tiered applications and we store no information in the UI. All data (before it is persisted) is stored in object structures that the UI references.

John,
- Jeff Marler B-)
 
hello jmarler,

the problem does not lie in any particular form. like i said, i tried to open every form thru the project browser and the out of memory error will appear on the 7th or 8th form.

i really do not wish to try and store the data elsewhere cos it would really involve a lot of work trying to save the data somewhere when the form is unloaded and trying to put the data back into the respective controls when the form needs to be loaded again. i have some thing like 20 to 30 textfields, comboboxes, checkboxes, etc...altogether PER form. bit of a tedious n redundant form of work rite?
 
Experiment by unloading those previous forms while testing and be sure to set the formName = Nothing e.g. if the form is known as frmOne then
Code:
Unload frmOne
set frmOne = nothing
Be sure AUTOREDRAW=false (defailt) is set on all forms, assuming you are not drawing all over it with graphical methods.
 
the out of memory error does not appear when i unload the previous forms before loading others, so does this mean i have to find a way to store the data and unload the forms?

the easiest way i can think of is declaring plenty (in number of 100s) of global variables to store these data but as john says, this is not the correct way to interface.

john, can you give me a brief example of how to use the Scripting.Dictionary? the search for this forum is currently down and i can't find any help.

lastly,i think the main point of this whole discussion is : How izit possible dat such small resource usage can lead to such an error?! no one seems to be able to solve this puzzle yet.

thank you for all you guys help
 
First, go to Start / Programs / Accessories / System Tools / Resource Meter. It will show up in the tray. Double click it. Now start running testing. check the resource meter. What is running down System / User / GDI? Mine is Ststem at User, neck in neck.

Project / References / Microsoft Scripting Runtime
Code:
'***** BAS MODULE Or class if you want several active
Private objDict as Scripting.Dictionary
' Constants to prevent misspellings slipping by
' dks DictionaryKeyString
' dka DictionaryKeyArray
Public Const dksName = "dksname"
Public Const dksOK = "dkaOK"
Public Const dksChecked = "dksChecked"
Public Function GetDictionary() as scripting.Dictionary
    if objDict = nothing then
        set objdict as scriting.Dictionary
        objDict.CompareMode = vbTextCompare ' Names have no case
    end if
    set GetDictionary = objDict
End sub
'*******
Form_Unload Sub
    Dim objDict as scripting.dictionary
    Set objDict = basmodule.GetDictionary()
    'ALWAYS ALWAYS SPECIFY PROPERTY OF CONTROL - NO DEFAULT
    ' objDict(dksName) = txtName ' stores reference to control on form
    ' and lots of trouble.
    ' For names you could use Control.Name
objDict(dksName) = txtName.text ' ALWAYS specify property
objDict(dksOK) = Cstr(optOK(0).Value = True) ' Stores string
objDict(dksChecked) =  Cstr(chkBox.Value = vbChecked)
End Sub

Reload_Form Sub
Dim objDict as scripting.dictionary
Set objDict = basmodule.GetDictionary()

txtName.text  = objDict(dksName) = ' ALWAYS specify property
optOK(0).Value = (lcase(objDict(dksOK)) = "true")
if lcase(objDict(dksChecked)) =  "true" then
    chkbox.value = vbChecked
else
    chkbox.value = vbUnChecked
end if
' combo box and list box selection is a another story.
End sub

The problem on reload is that CHANGE, CLICK etc events will be fired.

I HAD to use this beacuse my users can QUIT anywhere, anytime. exit everthing, shut the machine down, come back and start right where tey left off.
When we get to VB.NET, some of this "persistence" will be automatic through XML and a CLASSReloading property.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top