How to memory management in VB 6
Hi
I am a VB6 beginner and I program in C and C++ before. I want to know how to manage memory space in VB6. Just like in C, it provides a function that will clear a room in a memory by using free() function. How am I gonna do it in VB 6? is there a way or using KERNEL32, something like that, to manage memory?
Thanks a lot vbforums
Re: How to memory management in VB 6
Depends on what you want. In most cases you don't need to worry about memory management, VB does memory allocation and deallocation for you automatically when handling intelligent datatypes such as strings and arrays, or garbage collections such as Collection or Dictionary. If you however need to pass memory for something, you most often can use GlobalAlloc, GlobalFree, GlobalLock and GlobalUnlock.
Because VB6 lacks use of pointers like you may know them from C/C++, you need to either use RtlMoveMemory to fill the created memory space or use advanced hacks such as creating your own safe array header with a pointer to the memory space and fake an array variable to point to that header. This allows for a much faster access than RtlMoveMemory (due to some extra overhead involved with API calls in VB6).
The interesting part with most of VB's inbuilt string functions is that they're actually quite fast. If you need to handle large amounts of memory, for example swap them around in place, making a fake string can give very fast results. If you're filling a string there are ways to allocate the string faster. Normally all strings VB allocates are zeroed which is unnecessary to do if you just need a memory space to fill with something.
So, if you can be more specific on what you want or why you want it, we're probably able to help with that :)
Re: How to memory management in VB 6
I have a problem for my client/server database application.
It's need restarting Windows every two or three days to resolve "Out of memory" error message on the server.
Is there any soution for this ?
Re: How to memory management in VB 6
You should have started a thread of your own, as it is mostly unrelated to this thread.
Anyway, you have a memory leak in your program. Some part of the program keeps on reserving memory, but it does not release it. Check the sizes of your arrays, if you use API calls make sure you free what you create etc.
For further info take a look into the FAQ forum, there is a detailed post on how to handle this kind of issues.
Re: How to memory management in VB 6
You could always make a set of memory allocate/reallocate/free functions, using kernel32 like so;
vb Code:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef Source As Any, ByVal numbytes As Long)
Public Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (ByRef destination As Any, ByVal numbytes As Long)
Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Function malloc(ByVal dwSize As Long) As Long
Dim lngHandle As Long
lngHandle = GlobalAlloc(0, dwSize + 4)
malloc = GlobalLock(lngHandle) + 4
Call CopyMemory(ByVal malloc - 4, lngHandle, 4)
End Function
Public Sub free(ByVal dwPtr As Long)
Dim lngHandle As Long
Call CopyMemory(lngHandle, ByVal dwPtr - 4, 4)
Call GlobalUnlock(lngHandle)
Call GlobalFree(lngHandle)
End Sub
I've persionaly use'd this function alot in the past -- it can really speed things up with dealing with large data blocks.
One thing that springs to mind, is reading data from a 10MB file.
allocating a 10mb byte array in vb, will be rather slow, since vb nulls the memory before use. 200ms springs to mind. If said memory does not need to be null'ed, you can allocate 10mb of memory pretty much isntantly.
In the example above, I allocate an extra 4 bytes, to store the handle to said memory, in the 1st 4 bytes, then return an address just past it.
You may also want to store other infomation, like size of memory, etc etc.
Hope this helps
Re: How to memory management in VB 6
Thanks a lot BBN1 and Merri, such a good programmers. But, is it true that you can free memory by doing, Set object = nothing or strVar = vbnullstring?? Is this neccessary?
Thanks
Re: How to memory management in VB 6
Quote:
Originally Posted by
Krizaaa
Thanks a lot BBN1 and Merri, such a good programmers. But, is it true that you can free memory by doing, Set object = nothing or strVar = vbnullstring?? Is this neccessary?
Thanks
Yes it is true, and generally speaking, no it is not necessary.
When setting something to Nothing, if that was the last instance of that object, then the object is unloaded and some memory is freed. If the string is set to vbNullString, you are freeing the memory used by the string characters. If the variables are declared Public in a bas module, then you may want to free these when no longer needed, else they won't be freed until your app closes. If they are private to a routine, the memory will be freed when the routine exits. If declared in a form/class/usercontrol declarations section, then they will be freed when that form/class/usercontrol is unloaded.
P.S. Unless you are using strings that are eating up MBs of memory, you may be worrying about nothing. Objects should be destroyed (i.e. set to Nothing) when no longer needed in my opinion.
Re: How to memory management in VB 6
Wow..thanks lavolpe.i thank you all for your tips. im looking for this answers for long time.now its clear to me. i hope some viewers will benefit for this post. :duck:
Re: How to memory management in VB 6
Quote:
Originally Posted by
BBN1
You could always make a set of memory allocate/reallocate/free functions, using kernel32 like so;
vb Code:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef Source As Any, ByVal numbytes As Long)
Public Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (ByRef destination As Any, ByVal numbytes As Long)
Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Function malloc(ByVal dwSize As Long) As Long
Dim lngHandle As Long
lngHandle = GlobalAlloc(0, dwSize + 4)
malloc = GlobalLock(lngHandle) + 4
Call CopyMemory(ByVal malloc - 4, lngHandle, 4)
End Function
Public Sub free(ByVal dwPtr As Long)
Dim lngHandle As Long
Call CopyMemory(lngHandle, ByVal dwPtr - 4, 4)
Call GlobalUnlock(lngHandle)
Call GlobalFree(lngHandle)
End Sub
I've persionaly use'd this function alot in the past -- it can really speed things up with dealing with large data blocks.
One thing that springs to mind, is reading data from a 10MB file.
allocating a 10mb byte array in vb, will be rather slow, since vb nulls the memory before use. 200ms springs to mind. If said memory does not need to be null'ed, you can allocate 10mb of memory pretty much isntantly.
In the example above, I allocate an extra 4 bytes, to store the handle to said memory, in the 1st 4 bytes, then return an address just past it.
You may also want to store other infomation, like size of memory, etc etc.
Hope this helps
How can I use it inside my program ?