Re: Out of Memory Problem in VB 6.0 Application.



I just remembered that dynamic arrays are pointers to SAFEARRAY structure,
which is 24 bytes in length. So memory usage is as follows:

' 12 Bytes
Public Type TestType
p1 As Long
p2 As Long
p3() As Double ' 4 bytes pointer only
End Type

Public testVar() As TestType

Redim testVar(1 To 60000) As TestType
' Total memory usage now =
' 24 + 60000*12 + 60000 * 24 = 2,160,024 Bytes
For i = 1 To 60000
Redim testVar(i).p3(1 To 500) As Double
Next
' Total memory usage now =
' 2,160,024 + 8 * 500 * 60000 = 242,160,024 Bytes

I used Excel for the last line :-)

Here are some details about how VB stores dynamic arrays:

http://support.microsoft.com/default.aspx?scid=kb;en-us;205277#XSLTH4162121122120121120120

Also:
http://support.microsoft.com/kb/199824/


"Michael C" <mculley@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ubd0q49vFHA.916@xxxxxxxxxxxxxxxxxxxxxxx
> "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx> wrote in message
> news:dgp6ph$39v$1@xxxxxxxxxxxxxxxxxxxxxxx
>> I don't think so, Michael. Try this:
>>
>> Option Explicit
>> Private Type test
>> p1 As Long
>> p2 As Long
>> p3(1 To 500) As Double
>
> Of course, that's a fixed size array. If you declare a dynamic array, as
> Peri did, it will be a pointer. If you think about it, having a dynamic
> array inline with the type would be impossible (or very difficult) becuase
> the elements would not be evenly spaced in memory and higher elements
> would need to be moved every time you resized the array.
>
>> By the way, thanks for the info about VB arrays requiring a contiguous
>> block of memory. I'm
>> still a little confused about it though, because on my system I can
>> create a massive array that
>> requires much more real memory than I have on board and that can actually
>> approach the total of
>> real memory and swap file (on disk) memory. So can I take it that Windows
>> memory management is
>> able to treat a block of real RAM and a separate block of swap file (on
>> disk) as one complete
>> contiguous block, and that the CopyMemory API would do so also? Just
>> wondering.
>
> Just think of it as 2gigs of ram that you have at your disposal. It
> doesn't matter if that's real ram or swap file it's still 2 gigs available
> to your app. Windows and the cpu goes through some tricks to make your app
> think it's got this one big continuous area of ram. To confuse matters
> more the 2 gigs won't map directly to the same addresses in ram, eg
>
> addres 0 to 127 might be in a swap file
> 128 to 255 might be at 0x10000 in ram.
>
> The problem you get is if you then allocate some small chunks of that 2
> gigs at, say, 1 meg spacing all the way through the 2 gig, then you
> wouldn't be able to allocate anything bigger than 1 meg even though you've
> used up very little space.
>
> Michael
>


.



Relevant Pages

  • Re: Out of Memory Problem in VB 6.0 Application.
    ... that's a fixed size array. ... > block of memory. ... Just think of it as 2gigs of ram that you have at your disposal. ... matter if that's real ram or swap file it's still 2 gigs available to your ...
    (microsoft.public.vb.general.discussion)
  • Re: Handling large amounts of data
    ... > stereo sound sample stored at 4bytes per sample, ... > array syntax. ... Writing the data to disk and then memory mapping the files ... of RAM, and 40MB is nothing. ...
    (comp.lang.c)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Out of memory when reading a large file
    ... I would ideally like to read the data in an array and then be able to ... In order to do what you want, you need enough RAM on your computer. ... To get all the data into memory at once, ... chop up the data into smaller chunks. ...
    (comp.soft-sys.matlab)
  • Re: High Memory Consumption of Classes and Arrays
    ... Only the array itself has overhead. ... memory as a reference type. ... > least consume 40 bytes of memory. ...
    (microsoft.public.dotnet.framework.performance)

Quantcast