Results 1 to 9 of 9

Thread: Variable memory usage in Simba

  1. #1
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default Variable memory usage in Simba

    Simba Code:
    var
      x, y: String;
      a: integer;
      b: array[0..3] of char;
      z: t2dintegerarray;
    begin
      y:= '123123123123';
      writeln(sizeof(x));
      writeln(sizeof(y));
      writeln(sizeof(a));
      writeln(sizeof(b));

      setLength(z, 100);
      setLength(z[0], 100);
      writeln(sizeof(z));
    end.
    end.
    1. SizeOf() returns 4 for all of them, so does it mean that they all took up exactly the same amount of RAM during run time? (4bytes each?) esp the last variable 'z'?

    2. Is it true in simba that 4 byte data types have better speed performance than say, 2 byte data types due to data alignment etc?
    So for example, int32 should be used over int8 unless memory is of greater concern than speed?

    3. Is RAM consumed for declared methods too (both unused/invoked)? Exactly how much RAM would be used for, say, a null method?

    @nielsie95
    @Brandon
    @Dgby714

  2. #2
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    SizeOf is not dynamic, aka it wont work on dynamic arrays (and strings) you can try it with this;

    Simba Code:
    var
      ints: array [0..9999] of Integer;
      ints2: TIntegerArray;
    begin
      SetLength(ints2, 9999);
      Writeln(SizeOf(ints2));
      Writeln(SizeOf(ints));
    end;

  3. #3
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    1. Strings and DynArrays are pointers to some memory. SizeOf will then return the size of the pointer, which on 32bit is 4 bytes, and on 64bit (simba) is 8 bytes.
    To get the number of bytes allocated for some 1D array you can do this:
    Size := Length(Arr)*SizeOf(Arr[0])
    And for string types (AnsiString, WideString, UnicodeString etc):
    Size := Length(Str)*SizeOf(Str[1])
    Tho, AnsiString, and AnsiChar arrays are 1 byte per element, so the number of bytes used in those is equal to their length.

    However if you have declared a static array or string, for example:
    var str:String[10];
    then SizeOf(Str) will tell you that it's 11 bytes, as it known at compile-time.

    2. In any compiled language that will depend on several factors. And still the difference is microscopic.
    - In Lape as it's interpreted any real difference will not be noteable, use whatever fits the task.

    3. Yes, unused methods takes up some amount of space. Size depends. All the variables (their sizes) and the size of all the instructions in the method. etc...


    Btw: Do you have an issue with using Google? A few searches should probably give you the answers you need. Even tho some of this is directly lape-related, it doesn't change much from other compilers.
    Last edited by slacky; 11-28-2014 at 07:40 AM. Reason: Spelling
    !No priv. messages please

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    For 2 and 3:

    No and Yes. No it makes no difference for Simba because it's interpreted and everything is pushed through a stack and parsed manually.. It doesn't go through the buses directly. It doesn't get stored in registers or anything directly. You're not writing for embedded systems so don't worry about it. You're writing a script. Not assembly or low level code. You're more likely to have cache problems irl compared to size problems and memory constraints (depends). For scripts, just worry about making your own code efficient and like Slacky said, use whatever fits.

    Yes (all methods take memory regardless of whether they are un-used or empty) because it is interpreted and there's no optimiser (as done in JIT cases). O2, O3, O4, OFast, Os, etc.. do not exist in Simba. Any code you write, is as optimised as much as you manually did it.. If you did none, there is none. It would probably take longer to compile since it has parse that every time you hit play (though it's negligible). In a compiled language, it would remove it completely from the executable and generate the final binary.

    TDLR: Just code.. make your stuff efficient and don't worry about finer/minor details.
    Last edited by Brandon; 11-14-2014 at 04:17 PM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    733
    Mentioned
    81 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    1. ?? ?????? ??? ?? ?????????? ????? ??????? ?? ?????? ?????. Sizeof ???? ?? ?????????? ?? ??????? ??? ??????, ? ????? ?? 32bit ????? 4 bytes, ??? ??? 64bit (Simba) ????? 8 bytes.
    ??? ?? ?????? ??? ?????? ??? bytes ??? ??????????? ??? ?????? 1D ????? ???????? ?? ?? ?????? ????:
    ???????: = ????? (???) * sizeof (??? [0])
    ??? ??? ???? ?????? ????? (AnsiString, WideString, UnicodeString ???):
    ???????: = ????? (Str) * sizeof (Str [1])
    ?????????? Tho, AnsiString ??? AnsiChar ????? 1 byte ??? ????????, ???? ???? ? ??????? ??? bytes ??? ???????????????? ?? ????? ??? ????? ??? ?? ?? ????? ????.

    ??????, ?? ????? ??????? ??? ??????? ?????? ? ???????, ??? ??????????:
    var str: String [10]?
    ???? sizeof ?? ??? ??? ??? ????? 11 bytes, ???? ????? ?????? ???? ?? ???????????? ?????.

    2. ?? ???? ?????? ??? ????????????? ?? ????????? ??? ????????? ??????????. ??? ????? ? ??????? ????? ????????????.
    - ??? ?.?.?.?. ???? ???? ?????????? ??????????? ?????????? ??????? ??? ?? ????? noteable, ????? ????????? ????????? ?? ?? ????.

    3. ???, ?????????????? ???????? ???????????? ?????? ???? ??? ?????. ??????? ?????????. ???? ?? ?????????? (?? ?????? ????) ??? ?? ??????? ??? ???? ??? ??????? ??? ??????. ??? ...


    Btw: ????? ?????? ???????? ?? ?? ????? ??? Google; ???? ??????????? ?? ?????? ??????? ?? ??? ????? ??? ?????????? ??? ??????????. ????? ??? ?? ??????? ?????? ??? ???? ????? ????? ?.?.?.?. ??? ??????????? ??, ???? ??? ??????? ????? ??? ?????? ??????????????.
    90% of Slacky posts for me

  6. #6
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by ross View Post
    90% of slacky posts for me
    What? lol
    Last edited by slacky; 11-14-2014 at 05:39 PM.
    !No priv. messages please

  7. #7
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    What? lol
    I think he was pointing out, that your knowledge of these things/programming in general just totally blows his mind.

  8. #8
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Olly View Post
    SizeOf is not dynamic, aka it wont work on dynamic arrays (and strings) you can try it with this;

    Simba Code:
    var
      ints: array [0..9999] of Integer;
      ints2: TIntegerArray;
    begin
      SetLength(ints2, 9999);
      Writeln(SizeOf(ints2));
      Writeln(SizeOf(ints));
    end;
    Ahh that explains why changing length didnt change the sizeof result...

    Quote Originally Posted by slacky View Post
    1. Strings and arrays are pointers to some memory. SizeOf will then return the size of the pointer, which on 32bit is 4 bytes, and on 64bit (simba) is 8 bytes.
    To get the number of bytes allocated for some 1D array you can do this:
    Size := Length(Arr)*SizeOf(Arr[0])
    And for string types (AnsiString, WideString, UnicodeString etc):
    Size := Length(Str)*SizeOf(Str[1])
    Tho, AnsiString, and AnsiChar arrays are 1 byte per element, so the number of bytes used in those is equal to their length.

    However if you have declared a constant array or string, for example:
    var str:String[10];
    then SizeOf will tell you that it's 11 bytes, as it known at compile-time.

    2. In any compiled language that will depend on several factors. And still the difference is microscopic.
    - In Lape as it's interpreted any real difference will not be noteable, use whatever fits the task.

    3. Yes, unused methods takes up some amount of space. Size depends. All the variables (their sizes) and the size of all the instructions in the method. etc...


    Btw: Do you have an issue with using Google? A few searches should probably give you the answers you need. Even tho some of this is directly lape-related, it doesn't change much from other compilers.
    Dgby taught me to do sth like length * arr[0] while trying to compare 2 arrays...now i finally understand exactly what's going on....
    Thanks for the explanation!
    I didnt google because i was worried that other languages/compilers differ from simba/lape so i might just be getting the wrong ideas...

    Quote Originally Posted by Brandon View Post
    For 2 and 3:

    No and Yes. No it makes no difference for Simba because it's interpreted and everything is pushed through a stack and parsed manually.. It doesn't go through the buses directly. It doesn't get stored in registers or anything directly. You're not writing for embedded systems so don't worry about it. You're writing a script. Not assembly or low level code. You're more likely to have cache problems irl compared to size problems and memory constraints (depends). For scripts, just worry about making your own code efficient and like Slacky said, use whatever fits.

    Yes (all methods take memory regardless of whether they are un-used or empty) because it is interpreted and there's no optimiser (as done in JIT cases). O2, O3, O4, OFast, Os, etc.. do not exist in Simba. Any code you write, is as optimised as much as you manually did it.. If you did none, there is none. It would probably take longer to compile since it has parse that every time you hit play (though it's negligible). In a compiled language, it would remove it completely from the executable and generate the final binary.

    TDLR: Just code.. make your stuff efficient and don't worry about finer/minor details.
    Seems like i have to revamp the entire include and take out every method that i have never used...
    I'm running several instances of simba so even a 10mb increase per simba makes quite a significant difference.
    Right now each simba would take about 60-70MB and sometimes goes up to 100-200mb+ after a few days of running...i have no idea why it increases since i'm pretty sure there's no leak in my code...

    Also when i run a clean simba the memory usage increase from about 9mb to 25mb. Is this 16mb increase mostly due to the inbuilt simba methods/variables? So if i remove unused methods from simba and recompile it i could reduce yet another 10mb+ per simba?

    It's always the 3 of you that answer my incessant questions...really appreciates the help, would have repped all if i'm able to (http://puu.sh/cR1j7/ac354e3955.png)..

  9. #9
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    It's always the 3 of you that answer my incessant questions...really appreciates the help, would have repped all if i'm able to (http://puu.sh/cR1j7/ac354e3955.png)..
    Done.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •