• Welcome to Valhalla Legends Archive.
 

RAM Usage Help Needed

Started by Anubis, June 30, 2004, 06:49 PM

Previous topic - Next topic

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.

hismajesty

#1
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.

Anubis

Here's some code from one of my bots...

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


I'll try those things you mentioned :) Thanks

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?

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?

Anubis

#5
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.....

Banana fanna fo fanna

All I can suggest is use data structures whenever possible, NOT gui components.

Anubis

By data structures I assume you mean arrays and such?...

Stealth

Quote from: Anubis on June 30, 2004, 09:55 PM
By data structures I assume you mean arrays and such?...

The stuff they teach you in the VB books we all continuously recommend that you purchase.
- Stealth
Author of StealthBot

BinaryzL

Quote from: hismajesty[yL] on June 30, 2004, 07:20 PM
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.

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

Lenny

Quote
Edit: Oh yea, convert modules to class modules.

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.

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...
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

Eric

#11
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.

UserLoser.

Quote from: Anubis on June 30, 2004, 06:49 PM
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.

Do everything that VB sucks at in a DLL written in C++

Networks

#13
Quote from: LoRd[nK] on July 01, 2004, 02:31 AM
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.

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.

Eli_1

Quote from: Networks on July 02, 2004, 10:18 AM
How do you kill them when they are no longer need? Unload <cls> ?

I think Set <cls> = Nothing.