Valhalla Legends Forums Archive | Visual Basic Programming | RAM Usage Help Needed

AuthorMessageTime
Anubis
Most of the programs I make end up using a LOT of RAM and do hardly anything. The bot I've been working on lately has hardly anything in it and it uses over 8MB of RAM. y only question is what can I do / should I not do to lower this. Some API I should or shouldn't use, ActiveX control maybe? I've declared all variables that I've used (such as Dim i As Integer) and all variables have been delcared at the beginning of the functions...

Any help is much appreciated, thanks.
June 30, 2004, 11:49 PM
hismajesty
Convert functions that should be subs to subs, longs that should be integers to integers. Avoid unneeded control items (such as a listview/box for queue.) Convert all "" to vbNullStrings, and all " " to Space(1)s. There's other stuff too, perhaps you should post a code snippet so we can see your style?

Edit: Oh yea, convert modules to class modules.
July 1, 2004, 12:20 AM
Anubis
Here's some code from one of my bots...
[code]
Public Function DisplayList(LstBox As ListBox, Message As String)
Dim NewStr As String, i As Integer
i = 0
NextString:
NewStr = Message & ": "
Do Until Len(NewStr) >= 230 Or i > (LstBox.ListCount - 1)
If NewStr <> Message & " " Then NewStr = NewStr & ", " & LstBox.List(i)
If NewStr = Message & ": " Then NewStr = NewStr & LstBox.List(i)
i = i + 1
Loop
NewStr = Replace(NewStr, Message & ": ,", Message & ":")
NewStr = Replace(NewStr, " ", ":")
NewStr = Replace(NewStr, Message & "::", Message & ": ")
If NewStr = Message & ": " Then NewStr = NewStr & "(none)"
SendChat NewStr
If i < (LstBox.ListCount - 1) Then GoTo NextString
End Function
[/code]

I'll try those things you mentioned :) Thanks
July 1, 2004, 1:02 AM
hismajesty
That code right there displays some of the suggestions I said. You're taking up extra memory by storing " ", and you have a function even though that should be a sub. By the way, the default value for a variable is 0, why set i = 0 since you're redeclaring it everytime that function is called anyway?
July 1, 2004, 1:15 AM
Anubis
Sometimes when i don't assign i = 0 and I try to use it, it gives me the error or "Object or with block variable not set"...so I usually end up addigning i = 0 just to avoid the errors. Also, how do I figure out if something should be a sub or a function?
July 1, 2004, 2:43 AM
Anubis
The only reasons I'm even slightly concerned about the RAM usage are:

1) [New bot posted *anywhere*] Someone posts: "How much RAM does this bot use?"..."That's a lot for this bot..."
2) Simply trying to make it as fast as possible :)

I've also started a new bot and thought I'd try some ideas out to lower RAM usage hehehe... But you're right VB isn't good on the resources... An example of a HUGE RAM usage program is Starry Night[made in VB iirc] (my mom uses it). It drains our laptop battery in about 20 minutes...Of course it also uses a lot more of the processor too.....
July 1, 2004, 2:52 AM
St0rm.iD
All I can suggest is use data structures whenever possible, NOT gui components.
July 1, 2004, 2:54 AM
Anubis
By data structures I assume you mean arrays and such?...
July 1, 2004, 2:55 AM
Stealth
[quote author=Anubis link=board=31;threadid=7516;start=0#msg68031 date=1088650554]
By data structures I assume you mean arrays and such?...
[/quote]

The stuff they teach you in the VB books we all continuously recommend that you purchase.
July 1, 2004, 5:27 AM
BinaryzL
[quote author=hismajesty[yL] link=board=31;threadid=7516;start=0#msg68009 date=1088641246]
Convert functions that should be subs to subs, longs that should be integers to integers. Avoid unneeded control items (such as a listview/box for queue.) Convert all "" to vbNullStrings, and all " " to Space(1)s. There's other stuff too, perhaps you should post a code snippet so we can see your style?

Edit: Oh yea, convert modules to class modules.
[/quote]

I thought class modules would use more memory since it has more "things" than a regular module.
July 1, 2004, 6:06 AM
Lenny
[quote]
Edit: Oh yea, convert modules to class modules.
[/quote]

Modules and Class modules both have their respective uses, you shouldn't pointlessly convert *.bas modules which should be *.bas modules into class modules just to save ram. The CPU usage isn't a worthwhile trade....Also, in my opinion, thats poor programming etiquette.

[quote]
I thought class modules would use more memory since it has more "things" than a regular module.
[/quote]

I'm not certain if Class modules actually use more memory when they're loaded, but the idea that makes them more RAM efficient is that they're treated as objects unlike *.bas modules.
As such, they can be unloaded from memory...
July 1, 2004, 7:07 AM
LoRd
The only things that differ modules from class modules are that class modules can be loaded and unloaded on demand, can't contain public variables (other than functions, subs, properties and enumerators), and can be loaded more than once.
If you have a large number of functions/variables that aren't frequently being called, storing them in class modules and then killing them when they're no longer needed can be highly effective in freeing up RAM.
July 1, 2004, 7:31 AM
UserLoser.
[quote author=Anubis link=board=31;threadid=7516;start=0#msg68003 date=1088639344]
Most of the programs I make end up using a LOT of RAM and do hardly anything. The bot I've been working on lately has hardly anything in it and it uses over 8MB of RAM. y only question is what can I do / should I not do to lower this. Some API I should or shouldn't use, ActiveX control maybe? I've declared all variables that I've used (such as Dim i As Integer) and all variables have been delcared at the beginning of the functions...

Any help is much appreciated, thanks.
[/quote]

Do everything that VB sucks at in a DLL written in C++
July 1, 2004, 4:40 PM
Networks
[quote author=LoRd[nK] link=board=31;threadid=7516;start=0#msg68062 date=1088667082]
The only things that differ modules from class modules are that class modules can be loaded and unloaded on demand, can't contain public variables (other than functions, subs, properties and enumerators), and can be loaded more than once.
If you have a large number of functions/variables that aren't frequently being called, storing them in class modules and then killing them when they're no longer needed can be highly effective in freeing up RAM.
[/quote]

How do you kill them when they are no longer need? Unload <cls> ?

Anubis why don't you put Option Explicit on top all your modules, classes, and forms, then run the program and you'll notice all the variables you haven't declared and missed.
July 2, 2004, 3:18 PM
Eli_1
[quote author=Networks link=board=31;threadid=7516;start=0#msg68211 date=1088781517]
How do you kill them when they are no longer need? Unload <cls> ?
[/quote]

I think Set <cls> = Nothing.
July 2, 2004, 5:40 PM
Dyndrilliac
[quote author=Networks link=board=31;threadid=7516;start=0#msg68211 date=1088781517]Anubis why don't you put Option Explicit on top all your modules, classes, and forms, then run the program and you'll notice all the variables you haven't declared and missed.
[/quote]

That's more of error debugging then anything else.

What I did was make sure I unloaded and cleared all the objects I could in my programs after they would no longer be used. I made built in functions that would run when the program was idle to clear data and unload uneeded objects, made all my "" = vbNullString, all my " " = Space(1), and tried to use Longs as sparingly as possible.

Also use API over user made functions whenever possible. If you find yourself using lots of nested If/Then/Else statements, and it's basically choosing from multiple cases for a condition, convert those to Select Case statements. Also, use Loops instead of GoTo whenever possible.
July 2, 2004, 5:41 PM
Networks
Even if it is more debugging it will still help him to make sure ALL his variables are declared saving RAM right?
July 2, 2004, 6:34 PM
St0rm.iD
Yeah, variants are something ridiculously huge iirc.
July 2, 2004, 6:39 PM
Stealth
64 bytes freshly-initialized.
July 3, 2004, 12:42 AM
The-FooL
With multiple instances of a class module, I assume it doesn't reload all the functions does it? So if I had a class with a lot of functions and loaded two of them, would it take double the memory? The reason I'm asking this is because I have some very extensive class modules in my programs.
July 15, 2004, 12:47 PM
Grok
[quote author=The-FooL link=board=31;threadid=7516;start=15#msg70485 date=1089895653]
With multiple instances of a class module, I assume it doesn't reload all the functions does it? So if I had a class with a lot of functions and loaded two of them, would it take double the memory? The reason I'm asking this is because I have some very extensive class modules in my programs.
[/quote]

The code is loaded once. The data areas are created for each instance of your class.

If you load a class 10 times, the code will be in memory once, but you will have 10 data areas for the class data.
July 15, 2004, 4:48 PM
Maddox
There really is no difference between the memory required for a module and class module with the same implementation. If anything, the class takes a little more memory ( a few bytes ) to create a vtable for each instance of a class.

Like Grok said, your code is only in memory once, so freeing memory associated with a class because it has too many functions that are rarely used is not going to do much in terms of lowering your ram usage.

If you want to save memory then you should try to streamline your applications as best you can. Try to use as little amount of variables and code as possible. Clearing old text in text boxes can save a lot of memory as well.
July 17, 2004, 6:21 AM
Myndfyr
[quote author=UserLoser. link=board=31;threadid=7516;start=0#msg68085 date=1088700008]
[quote author=Anubis link=board=31;threadid=7516;start=0#msg68003 date=1088639344]
Most of the programs I make end up using a LOT of RAM and do hardly anything. The bot I've been working on lately has hardly anything in it and it uses over 8MB of RAM. y only question is what can I do / should I not do to lower this. Some API I should or shouldn't use, ActiveX control maybe? I've declared all variables that I've used (such as Dim i As Integer) and all variables have been delcared at the beginning of the functions...

Any help is much appreciated, thanks.
[/quote]

Do everything that VB sucks at in a DLL written in C++
[/quote]

Such as Newby, who decided that the Winsock control sucked, so he created a wrapper DLL in C that had a function that called a wrapper function to call send().
July 20, 2004, 2:04 AM
Newby
Hurr, I was told it was a dumb idea so I removed it. :( :P
July 20, 2004, 2:31 AM
hismajesty
[quote author=Myndfyre link=board=31;threadid=7516;start=15#msg71413 date=1090289059]
[quote author=UserLoser. link=board=31;threadid=7516;start=0#msg68085 date=1088700008]
[quote author=Anubis link=board=31;threadid=7516;start=0#msg68003 date=1088639344]
Most of the programs I make end up using a LOT of RAM and do hardly anything. The bot I've been working on lately has hardly anything in it and it uses over 8MB of RAM. y only question is what can I do / should I not do to lower this. Some API I should or shouldn't use, ActiveX control maybe? I've declared all variables that I've used (such as Dim i As Integer) and all variables have been delcared at the beginning of the functions...

Any help is much appreciated, thanks.
[/quote]

Do everything that VB sucks at in a DLL written in C++
[/quote]

Such as Newby, who decided that the Winsock control sucked, so he created a wrapper DLL in C that had a function that called a wrapper function to call send().
[/quote]

Haha, not only is the idea silly..so is the way it sounds when said verbally. :P
July 20, 2004, 2:33 AM
Grok
Pretty good idea, actually. That sounds like a terrific way to learn to write a decent, reliable library, one you would use yourself. At the same time, learning how an API works behind a commonly used ActiveX control (WinSock.dll).
July 20, 2004, 2:44 AM
LoRd
[quote]Also use API over user made functions whenever possible.[/quote]
Overuse of API calls can be a bad thing.
July 20, 2004, 3:41 AM
Myndfyr
[quote author=LoRd[nK] link=board=31;threadid=7516;start=15#msg71438 date=1090294899]
[quote]Also use API over user made functions whenever possible.[/quote]
Overuse of API calls can be a bad thing.
[/quote]

Unless all you do is consistently access wrappers to those API calls, in which case you'll likely reduce memory costs and increase speed by taking that level of indirection out.
July 21, 2004, 4:29 PM
St0rm.iD
At the same time, destroying what VB is good at, and increasing your development time.

Just code efficiently, and write speed-intensive portions in DLLs.
July 21, 2004, 5:06 PM

Search