Out of memory in VB6 when the Mem Usage is lower

Out of memory in VB6 when the Mem Usage is lower

Post by Nick » Fri, 17 Dec 2004 13:06:31


Hi all,
I am a newcomer for VB6. Here is the problem I have with my VB6
Application. Please help me to figure out what is the cause. Thanks in
advance.

I run the following code in XP OS. RAM 1GB/12 G free space in harddisk.
When the MAIN function call the Function1 in Class1, from the task
manager, I saw the Mem Usage grow up high to about 880M, then, after
the function1 finished correctly, it drop down as expected to 12M, so
is the virtual memory usage. The MAIN function keep on doing other
stuff, and the memory usuage as well as virtual memory grow up to about
700M, and then I got a out-of-memory.

My question is -- why I did not get an out-of-memory when the Mem Usage
went up to 800M, but got an out-of-memory when the Mem Usage is 700M?
is the way I clean up the array memory correct?

Thanks you very much
Nicky
---------------
within Class1.cls I have a function
Function1()
Dim arrayLong(0 To 3000000) As Long
Dim array2Long() As Long
Dim arrayString() as String

ReDim arrayString(3000000) As String
ReDim array2Long(3000000, 1) As Long

for i = 0 to 3000000
arrayString(i) = LoadLineFromATextFile
array2Long(i, 0) = SomeLongNumber
arrayLong(i) = SomeLongNumber
next i

.... use the arrays to do other handling

Erase array2Long, arrayString
exit
end function


MAIN
dim clsObj as Class1
set clsObj = new Class1
clsObj.Function1
set clsObj = nothing
... do other stuff which need allocate 100M memory
End MAIN
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Randy Birc » Fri, 17 Dec 2004 13:47:22

I'll bet the out of memory error is related to exceeding a maximum limit
with regard to the total size of string, and not a maximum of physical or
virtual memory.

--


Randy Birch
MS MVP Visual Basic
http://www.yqcomputer.com/




: Hi all,
: I am a newcomer for VB6. Here is the problem I have with my VB6
: Application. Please help me to figure out what is the cause. Thanks in
: advance.
:
: I run the following code in XP OS. RAM 1GB/12 G free space in harddisk.
: When the MAIN function call the Function1 in Class1, from the task
: manager, I saw the Mem Usage grow up high to about 880M, then, after
: the function1 finished correctly, it drop down as expected to 12M, so
: is the virtual memory usage. The MAIN function keep on doing other
: stuff, and the memory usuage as well as virtual memory grow up to about
: 700M, and then I got a out-of-memory.
:
: My question is -- why I did not get an out-of-memory when the Mem Usage
: went up to 800M, but got an out-of-memory when the Mem Usage is 700M?
: is the way I clean up the array memory correct?
:
: Thanks you very much
: Nicky
: ---------------
: within Class1.cls I have a function
: Function1()
: Dim arrayLong(0 To 3000000) As Long
: Dim array2Long() As Long
: Dim arrayString() as String
:
: ReDim arrayString(3000000) As String
: ReDim array2Long(3000000, 1) As Long
:
: for i = 0 to 3000000
: arrayString(i) = LoadLineFromATextFile
: array2Long(i, 0) = SomeLongNumber
: arrayLong(i) = SomeLongNumber
: next i
:
: .... use the arrays to do other handling
:
: Erase array2Long, arrayString
: exit
: end function
:
:
: >From MAIN modula, I define a class varible like this
: MAIN
: dim clsObj as Class1
: set clsObj = new Class1
: clsObj.Function1
: set clsObj = nothing
: ... do other stuff which need allocate 100M memory
: End MAIN
:

 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Tony Proct » Fri, 17 Dec 2004 19:45:47

andy may well be right Nicky. It depends a lot on what "other stuff" your
MAIN function happens to be doing, and how big those text records are from
your file.

I'm not totally sure of the cause here either, but the following background
may help clarify a common misconceptions (hope you don't mind):

All normal 32-bit applications can only address 2Gb of virtual memory. It
doesn't matter how much physical memory (RAM) or disk space you have. OK, so
your 700-800Mb is a lot smaller than this. Well, a few things automatically
get deducted from the maximum 2Gb, e.g. any DLLs you have loaded. These are
always loaded on 64k boundaries and so they're spaced out a bit (usually
system ones at high addresses and user ones at low addresses), but that
extra space isn't shown by the 'VM Size' value in Task Manager. That only
shows private contributions (i.e. from your own program) rather than shared
contributions (such as all EXE/DLL instructions).

OK, but you're going to say something like 'I don't have many DLLs'. Well,
there's another effect called "heap fragmentation", and this is a likely
issue for you because your arrays are large. The process heap(s) grow
dynamically to accommodate memory allocation for arrays and objects. When
memory is freed, the heap management usually holds it on a free-list (i.e.
still part of your process). Sometimes it can delete a local region of your
virtual address space, and so release any resources associated with it --
reallocating such memory from scratch happens to be more efficient then
re-using old memory from the free list. It's possible that a subsequent
request to allocate a large block of memory for an array can't find a
contiguous region big enough in the current heap (remember, there may be
several distinct heaps in a process), and this can result in an 'Out of
Memory' error. In effect, even though it looks like your total VM usage is
small, it may have been all broken up so that there are no contiguous areas
left.

Another issue concerns XP SP2. I've heard that the netapi32 DLL was re-based
for some off reason, and it now sits in the middle of nowhere rather than at
the high address is used to sit at. This can also increase the effects of
fragmentation. See this thread for instance:
http://www.google.ie/groups?safe=off&as_umsgid= XXXX@XXXXX.COM &lr=lang_en&hl=en

One last possibility - check the size of your system paging file. If this is
too small to accommodate the VM use of all active processes at that time
then it will restrict your process to less than the 2Gb theoretical max.
Again, this can result in 'Out of Memory'.

Tony Proctor

"Nicky" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...


 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Nick » Fri, 17 Dec 2004 23:52:57

Thanks a lot for reply Tony and Randy.

I created an application to do the things Class1 do. From MAIN, I run
and Class1 Application, and wait for the class1 application finish
before I go on to do the other stuff. Everything else keeps same as
before. In this case, I don't have out-of-memory. By the way, the
Maximum string length from the text file is about 500 chars.

Stilll hang on this problem because I don't want to run a process
inside MAIN applicate. Any input is appreciated.

Nicky
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Nick » Fri, 17 Dec 2004 23:56:54

Randy,
What is a maximum limit with regard to the total size of string? I
know there are some limitation, but in my case, the maximum length is
less than 1000 chars.
Thanks,
Nicky
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Tony Proct » Sat, 18 Dec 2004 00:39:21

If your process is CPU-bound then it may not be giving the VB run-time a
chance for the Class_Terminate events to fire. In other words, the classes
have be in a sort of "delete pending" state.

Try putting a DoEvents call after your "set clsObj = nothing".

Tony Proctor
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Nick » Sat, 18 Dec 2004 00:41:16

Tony,
I rethink the whole process, seems liket the problem is caused by the
heap fragment, as you said. Here is the other question:

Is there anyway I can do the de-fragment from my application? Any
workaround?

Thank you very much!!!
Nicky
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by Tony Proct » Sat, 18 Dec 2004 02:22:11

You can't force the heap to de-fragment. It's a question of application
design.

How are you sure that's the issue Nicky?

Did you try the DoEvents statement?

Tony Proctor
 
 
 

Out of memory in VB6 when the Mem Usage is lower

Post by neilaness » Sat, 18 Dec 2004 07:57:04

Nicky,
Do you really need to store 3M records in memory?
What are you attempting to achieve? Probably not necessary
to hold all that data in core.
Neila