|
-
Dec 10th, 2009, 09:35 AM
#1
Thread Starter
PowerPoster
Memory Usage
The key question for me is memory usage and recovery.
Am I correct in "assuming" the following:
1) String literals that reside and are compiled in the EXE remain in
memory throughout the life of the App.
2) String literals that are loaded at runtime (say from text file or
database) can be removed from memory depending on the object to which they are loaded For example if a text file is read into a MessageBox string and the procedure
where the Messagbox resides finishes, the string is removed from memory.
Other objects where strings are read to the object from a file are removed from memory once the object is: set = Nothing.
3) A Resource file is read and memory allocated when the EXE is compiled and
strings stays in memory -- no memory is recovered.
-
Dec 10th, 2009, 09:40 AM
#2
Re: Memory Usage
Seems your questions are string related.
Strings stay in memory while app runs only if the string is in a constant.
Otherwise, you can release the memory used by the string at any time by simply setting its value to vbNullString. String variables that go out of scope in a procedure are released when procedure exits. String variables that are declared a module level, i.e., form/class object declarations section, are released when that object is destroyed unless you null it out sooner. String variables declared as Public in a bas module are their for life of app unless you null it out sooner.
Regarding string tables in a resource file? I don't know that answer to that question, but would assume you are correct.
P.S. All memory used by strings are eventually recovered when the app terminates. Exceptions of course apply, especially when using APIs incorrectly to create/manipulate strings.
-
Dec 10th, 2009, 10:25 AM
#3
Thread Starter
PowerPoster
Re: Memory Usage
LaVolpe: Thanks for response.
Regarding:
Strings stay in memory while app runs only if the string is in a constant.
I assume here you are referring to:
Const strTest As String = "This is a string"
Are not all string literals defined within a procedure held in a String Table which is part of program memory throughout the life of the App?
For example:
Code:
Sub MyProcedure
Dim strPrompt As String
strPrompt = "This is a string"
End Sub
In fact now that I think of it I would imagine strings declared as "Const" would also be part of the string table.
Last edited by dw85745; Dec 10th, 2009 at 11:01 AM.
-
Dec 10th, 2009, 12:09 PM
#4
Re: Memory Usage
Simplified, a string is memory allocation used for a variable.
When strPrompt is declared, VB creates the variable and assigns it a default value, which in this case is vbNullString. At that point strPrompt contains no additional memory usage other than what VB needs to do to keep track of the variable itself.
Once you set a value to strPrompt, VB allocates memory equal to 2x its length + 4 bytes identifying its length, + 2 bytes for a double null terminating character. VB uses what is called a BSTR for strings, each char requires 2 bytes. Now strPrompt is using 22 bytes. When the procedure exits, VB releases the 22 bytes of memory plus whatever it needed to track strPrompt. All memory for strPrompt is now returned to the system.
FYI: vbNullString is zero bytes, however, the literal "" is actually 6 bytes because it is stored as 4 bytes for its length, plus 2 byte null terminator.
Regarding constants, you understood my statement correctly.
Here is a good article on VB storage.
The above comments pertain to string variables that are assigned during runtime.
Edited: A relatively easy test may be doable. Create a sample project that does two things: 1) create 1000's of strings, 2) sets those strings to vbNullString. Run the project, create the strings, look at task manager and memory usage, destroy strings, see if memory usage decreased.
 Originally Posted by dw85745
Are not all string literals defined within a procedure held in a String Table which is part of program memory throughout the life of the App?...
String literals that reside and are compiled in the EXE remain in memory throughout the life of the App.
You can't see me, but I'm tap dancing a bit and backtracking. I think I misunderstood your original question: The quoted part is true I believe. For example, if you had a piece of code that checked if some variable was equal to "Monday", and compiled your app, you would be able to find "M o n d a y" in the exe when viewing it in NotePad; another reason never to hardcode passwords
Last edited by LaVolpe; Dec 10th, 2009 at 01:07 PM.
-
Dec 10th, 2009, 01:47 PM
#5
Thread Starter
PowerPoster
Re: Memory Usage
LaVolpe: Thanks for response and link.
There seems to be a lot of confusion --based on what I've read in various places -- as to how the compiler is dealing with the allocation / deallocation of VB strings depending on how they are input and / or used.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|