Bigger array size - more RAM consumption?
#1

Hi guys,

I have a very simple question. Does bigger array size uses more RAM consumption? Example:

pawn Код:
new
    array1[ 2 ],
    array2[ 2000 ]
;
So array1 uses less RAM consumption than array2? And how to know, how much array1 or array2 uses RAM consumption(in bytes, kilobytes or megabytes)?
Reply
#2

Yes.

A pawn cell is 32 bit - 4 bytes, so if it's a global the script will use that memory pre-initialized in the amx file itself.
If it's a local, it will be added to the reserved stack portion, and everything ofcourse uses up ram..
Reply
#3

Thanks! But I don't understand other thing. Did array used RAM size frees after using it? Example:

pawn Код:
public OnPlayerConnect( playerid )
{
    new
        array[ 2000 ]
    ;
    // formating array, and doing something with it.
}
So connects 15 players. 15 * 2000 * 4 = 120000KB, so now the script uses 120000KB RAM? I'm sure it's not, so why listen this tutorial instead of using example 256? It's just a 1KB . Thanks for replies, I need arguments.
Reply
#4

Yes, memory is freed as soon as the function ends. The thing is: why would you need the extra space if you're not going to use anyway? As JernejL already pointed out, there is a reserved stack portion to which all local variables are saved during function calls. If you use too much large arrays, the stack can eventually overflow and cause all kind of errors where variables overwrite each other.
Reply
#5

Thanks a lot! If I can overflow the stack, what is the limit?
Reply
#6

15 kilobytes or so, I believe. You can check that using the heapspace() native. The function returns the space left on the stack/heap in bytes, so you'll need to divide by 1024 to get kilobytes.
Reply
#7

Thanks a lot for the info!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)