How to get heap / RAM usage of a program?

Posted on
  • Hello there,

    Does anyone know how I can get the heap or RAM usage of a program on Pixl?

    I'm trying to find out how much RAM or heap can be saved from applying the Performance Notes. Also trying to see how much RAM is used by the Espruino engine itself and by my program.

    Any help will be appreciated.

  • process.memory() is probably the most important one

    Also check http://www.espruino.com/Internals
    There is also E.dumpVariables or E.dumpFragmentation Also trace(variable) can dump a tree of everything referenced by variable.

    There is no heap, just variables. Also there is CPU stack growing down, the value can be seen in process.memory().stackEndAddress so 0x20000000+0x10000-process.memory().stac­kEndAddress can give size of currently(?) used stack.

  • Thanks a lot for your help! :)
    I played with process.memory() for a while on some toy code, but still no sure what usage and history mean.
    For example:

    const ramBefore = process.memory();
    const helloWorld = `${JSON.stringify(process.memory())} Hello, world!`;
    const ramAfter = process.memory();
    
    print(`RAM Before: ${JSON.stringify(ramBefore)}`);
    print(`RAM Middle: ${helloWorld}`);
    print(`RAM After: ${JSON.stringify(ramAfter)}`);
    

    Result:

    RAM Before: {"free":2383,"usage":117,"total":2500,"h­istory":6,"gc":0,"gctime":3.47900390625,­"blocksize":16,"stackEndAddress":5369295­92,"flash_start":0,"flash_binary_end":44­3732,"flash_code_start":446464,"flash_le­ngth":524288}
    RAM Middle: {"free":2336,"usage":164,"total":2500,"h­istory":11,"gc":0,"gctime":3.57055664062­,"blocksize":16,"stackEndAddress":536929­592,"flash_start":0,"flash_binary_end":4­43732,"flash_code_start":446464,"flash_l­ength":524288} Hello, world!
    RAM After: {"free":2330,"usage":170,"total":2500,"h­istory":19,"gc":0,"gctime":3.57055664062­,"blocksize":16,"stackEndAddress":536929­592,"flash_start":0,"flash_binary_end":4­43732,"flash_code_start":446464,"flash_l­ength":524288}
    

    If I understand the document correctly, then usage is the number of currently used blocks of RAM, and history is the number blocks used by all the the commands (JS statements) until inclusive the current statement, right?

  • Another example of fibonacci:

    print(`Initial: ${JSON.stringify(process.memory())}`);
    function fibonacci(num) {
      print(`num ${num}: ${JSON.stringify(process.memory())}`);
      if (num <= 1) return 1;
    
      return fibonacci(num - 1) + fibonacci(num - 2);
    }
    print(`Before: ${JSON.stringify(process.memory())}`);
    fibonacci(5);
    print(`After: ${JSON.stringify(process.memory())}`);
    

    Result:

    Initial: {"free":2379,"usage":121,"total":2500,"h­istory":0}
    Before: {"free":2373,"usage":127,"total":2500,"h­istory":0}
    num 5: {"free":2366,"usage":134,"total":2500,"h­istory":0}
    num 4: {"free":2360,"usage":140,"total":2500,"h­istory":0}
    num 3: {"free":2354,"usage":146,"total":2500,"h­istory":0}
    num 2: {"free":2348,"usage":152,"total":2500,"h­istory":0}
    num 1: {"free":2342,"usage":158,"total":2500,"h­istory":0}
    num 0: {"free":2341,"usage":159,"total":2500,"h­istory":0}
    num 1: {"free":2347,"usage":153,"total":2500,"h­istory":0}
    num 2: {"free":2353,"usage":147,"total":2500,"h­istory":0}
    num 1: {"free":2347,"usage":153,"total":2500,"h­istory":0}
    num 0: {"free":2346,"usage":154,"total":2500,"h­istory":0}
    num 3: {"free":2359,"usage":141,"total":2500,"h­istory":0}
    num 2: {"free":2353,"usage":147,"total":2500,"h­istory":0}
    num 1: {"free":2347,"usage":153,"total":2500,"h­istory":0}
    num 0: {"free":2346,"usage":154,"total":2500,"h­istory":0}
    num 1: {"free":2352,"usage":148,"total":2500,"h­istory":0}
    After: {"free":2373,"usage":127,"total":2500,"h­istory":0}
    

    It's fun to see how the RAM usage grows, but I don't find how I can get the peak RAM usage of the program without putting process.memory() on every step...

  • free+usage=total, history is command lines entered into console, available when pressing up key, those are cleared automatically when memory is low so are not counted into used space

  • Understood! Thanks a lot for your help :)

  • I don't find how I can get the peak RAM usage of the program without putting process.memory() on every step

    That's what you'd have to do I'm afraid - there's no way to get peak RAM usage without doing that. It's a bit tricky to get a solid figure for that because of garbage collection.

  • Understood! Thanks :)

  • there is also a builtin debugger, try to run debugger;, it can single step your code and print command can evaluate anything between steps so I guess some plugin in webIDE could script that and log 'print process.memory().used' after every step. So it could be done without changing your code but would be slow to run it like that.

  • Good idea! I tried debugger; with print manually. Looks like a good way. Will try some plugins or modifying the debugger code later to test on larger programs :D

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to get heap / RAM usage of a program?

Posted by Avatar for Ragtime @Ragtime

Actions